Bind Active Directory Objects to ASP.Net Web Controls, DropDownList or ListBox

by IISMATT 15. June 2009 09:39

I have had to grab the users from Active Directory and bind them to an ASP.NET Control in order to display them in web page, and after doing some testing and fishing around, I came across a method that worked for me after some adjustments and trial and error...

'vb.net CodeBehind===============================

 

Imports System.DirectoryServices

Partial Class _Default
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub btnBindUsers_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBindUsers.Click

        Dim ADPath As String = "LDAP://OU=Users,DC=yourdmain,DC=local"
        Dim dEntry As New DirectoryEntry(ADPath)
        Dim dSearcher As New DirectorySearcher(dEntry)

        ' You do not want to make this too large if you have a big directory
        dSearcher.PageSize = 250
        dSearcher.Filter = "(objectCategory=Person)"

        'Create array to sort records alphabetically
        Dim theUsers As ArrayList
        theUsers = New ArrayList
        Dim theResults As SearchResult

        For Each theResults In dSearcher.FindAll()
           theUsers.Add(theResults.Properties("cn")(0))
        Next theResults

        theUsers.Sort()
        'demo of a list box
        lstUser.DataSource = theUsers
        lstUser.DataBind()
        'demo of a DropDownList
        ddlUsers.DataSource = theUsers
        ddlUsers.DataBind()

        dEntry.Dispose()

    End Sub
End Class

 

'End vb.net CodeBehind===============================

Asp.Net HTML Page===================================

 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="DebtTracker._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnBindUsers" runat="server" Text="Show Users" />
        <br />
        <asp:ListBox ID="lstUser" runat="server" Height="175px" Width="150px" />
        <br />
        <asp:DropDownList ID="ddlUsers" runat="server" Height="20px" Width="150px" />
    </div>
    </form>
</body>
</html>

End Asp.Net HTML Page===================================

Shout it



kick it on DotNetKicks.com

vote it on WebDevVote.com
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: , , , , ,

ASP.NET

Comments

6/4/2009 11:53:08 PM #

trackback

Bind Active Directory Objects to ASP.Net Web Controls, DropDownList or

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com |

6/5/2009 9:39:49 AM #

trackback

Bind Active Directory Objects to ASP.Net Web Controls, DropDownList or

You are voted (great) - Trackback from WebDevVote.com

WebDevVote.com |

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

IIS M@

I is Matt

Protected by Commentor
comments approved
spam caught
Since December 1, 2008
Powered by Spam Counter