将所有用户从活动目录绑定到listview控件?

时间:2014-06-16 08:41:28

标签: c# asp.net active-directory

我正在尝试列出所有活动目录用户和电子邮件到ListView控件:

<asp:ListView ID="ListView1" runat="server">
        <ItemTemplate>
            <tr>
                <td></td>
                <td><%# Eval("UserID") %></td>
                <td><%# Eval("EmaifdglID") %></td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table>
                <tr>
                    <th></th>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
            </table>
        </LayoutTemplate>
    </asp:ListView>

我使用此代码获取数据:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        GetAllADUsers();
}

public void GetAllADUsers()
{
    try
    {

        DirectoryEntry myLdapConnection = new DirectoryEntry("LDAP://domainname");
        DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };
        search.CacheResults = true;
        SearchResultCollection allResults = search.FindAll();
        DataTable resultsTable = new DataTable();
        resultsTable.Columns.Add("UserID");
        resultsTable.Columns.Add("EmailID");
        foreach (SearchResult searchResult in allResults)
        {
            MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties["sAMAccountName"][0].ToString()];
            if (myUser == null)
            {
                //Defining the RAW data
                DataRow dr = resultsTable.NewRow();

                //Adding the User ID
                dr["UserID"] = searchResult.Properties["sAMAccountName"][0].ToString();

                //Adding the Email address
                if (searchResult.Properties["mail"].Count > 0)
                {
                    dr["EmailID"] = searchResult.Properties["mail"][0].ToString();
                }
                else
                {
                    dr["EmailID"] = "";
                }

                //Filling the Raw To DataTable
                resultsTable.Rows.Add(dr);

            }
            else
            { }
        }
        if (resultsTable.Rows.Count != 0)
        {
            ListView1.DataSource = resultsTable;
            ListView1.DataBind();
        }
        {
            Response.Write("No Data");
        }


    }
    catch (Exception)
    {
    }
}

我跟踪了代码,发现大约有1000个用户: enter image description here

但我仍然没有得到任何东西! 问题在哪里?

1 个答案:

答案 0 :(得分:1)

对不起大家,我明白了。

我忘了为listview添加itemplaceholder:

<tr id="itemPlaceholder" runat="server"> </tr>

这是完整的

 <asp:ListView ID="ListView1" runat="server">
        <ItemTemplate>
            <tr>
                <td></td>
                <td><%# Eval("UserID") %></td>
                <td><%# Eval("EmailID") %></td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table>
                <tr>
                    <th></th>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
                  <tr id="itemPlaceholder" runat="server">
                 </tr>
            </table>
        </LayoutTemplate>
    </asp:ListView>