开发者

Search AD with textbox value and return results as datatable

开发者 https://www.devze.com 2023-01-18 06:09 出处:网络
Currently I have a gridview bound to a datatable which is populated with groups from the AD. I need to be able to add search functionality so users can type in part of a group name and have the result

Currently I have a gridview bound to a datatable which is populated with groups from the AD. I need to be able to add search functionality so users can type in part of a group name and have the results display only groups that fit their search criteria. Here's what I have so far.

<asp:TextBox ID="searchParam" runat="server"></asp:TextBox><asp:button ID="btnSearch" runat="server" Text="Search" />
<asp:GridView ID="dgSearchDLs" runat="server" AutoGenerateColumns="False" DataKeyNames="cn" DataSourceID="ObjectDataSource1">
    <Columns>
        <asp:BoundField DataField="cn" HeaderText="DL Name"/>
        <asp:BoundField DataField="managedBy" HeaderText="Managed By"/>
        <asp:BoundField DataField=开发者_C百科"info" HeaderText="Notes"/>
        <asp:ButtonField ButtonType="Button" text="Add" HeaderText = "Select DL" CommandName="AddDL"  />
    </Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    SelectMethod="getCOMDLs" TypeName="NewEmployee">
</asp:ObjectDataSource>

NewEmployee class:

    Function getCOMDLs() As DataTable

    Dim MySearchRoot As DirectoryEntry = New DirectoryEntry("path", "usr", "pwd")
    Dim MyDirectorySearcher As New DirectorySearcher(MySearchRoot)
    Dim strManagedBy As String

    MyDirectorySearcher.Filter = ("(&(objectCategory=group)(|(name=dl*)))")


    MyDirectorySearcher.SearchScope = SearchScope.Subtree
    MyDirectorySearcher.PropertiesToLoad.Add("cn")
    MyDirectorySearcher.PropertiesToLoad.Add("ManagedBy")
    MyDirectorySearcher.PropertiesToLoad.Add("info")
    MyDirectorySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
    MyDirectorySearcher.Sort.PropertyName = "cn"

    Dim MySearchResult As SearchResultCollection = MyDirectorySearcher.FindAll()

    Dim myTable As New DataTable("Results")
    Dim colName As String
    Dim i As Integer

    For Each colName In MyDirectorySearcher.PropertiesToLoad
        myTable.Columns.Add(colName, GetType(System.String))
    Next

    Dim result As SearchResult

    For Each result In MySearchResult
        Dim dr As DataRow = myTable.NewRow()
        For Each colName In MyDirectorySearcher.PropertiesToLoad
            If result.Properties.Contains(colName) Then
                If colName = "ManagedBy" Then
                    strManagedBy = CStr(result.Properties(colName)(0))
                    i = strManagedBy.IndexOf(",")
                    strManagedBy = strManagedBy.Substring(3, i - 3)
                    dr(colName) = strManagedBy
                Else
                    dr(colName) = CStr(result.Properties(colName)(0))
                End If
            Else
                dr(colName) = ""
            End If
        Next
        myTable.Rows.Add(dr)
    Next
    Return myTable
End Function


The solution was using a FormParameter control to pass the value of the text field and I had to set the name (searchArray) property that was being used as the variable name in the function:

<asp:TextBox ID="searchParam" runat="server"></asp:TextBox><asp:button ID="btnSearch" runat="server" Text="Search" />
<asp:GridView ID="dgSearchDLs" runat="server" AutoGenerateColumns="False" DataKeyNames="cn" DataSourceID="ObjectDataSource1">
<Columns>
    <asp:BoundField DataField="cn" HeaderText="DL Name"/>
    <asp:BoundField DataField="managedBy" HeaderText="Managed By"/>
    <asp:BoundField DataField="info" HeaderText="Notes"/>
    <asp:ButtonField ButtonType="Button" text="Add" HeaderText = "Select DL" CommandName="AddDL"  />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getCOMDLs" TypeName="NewEmployee">
    <asp:FormParameter FormField="searchParam" Type="String" DefaultValue="" Name="searchArray" />
</asp:ObjectDataSource>

Codebehind for the search button: The loops below remove all spaces before passing the string array to the function

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    Dim obj As New NewEmployee
    Dim dt As DataTable
    Dim searchStr As String = searchParam.Text
    Dim tempStrArr() As String
    Dim searchStrArr() As String = Nothing
    Dim searchStrCt = 0

    If Not searchStr Is Nothing Then
        tempStrArr = searchStr.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
        ReDim searchStrArr(0 To tempStrArr.Length - 1)

        For i As Integer = 0 To tempStrArr.Length - 1
            If tempStrArr(i) <> "" Then
                searchStrArr(searchStrCt) = tempStrArr(i)

                searchStrCt += 1
            End If
        Next
    End If

    dt = obj.getCOMDLs(searchStrArr)
    If obj.ErrMessage = "" Then
        If dt.Rows.Count >= 0 Then
            dgSearchDLs.DataSourceID = ""
            dgSearchDLs.DataSource = dt
            dgSearchDLs.DataBind()
        End If
    End If
End Sub

The NewEmployee class:

 Function getCOMDLs(Byval searchArray as string()) As DataTable

Dim MySearchRoot As DirectoryEntry = New DirectoryEntry("path", "usr", "pwd")
Dim MyDirectorySearcher As New DirectorySearcher(MySearchRoot)
Dim strManagedBy As String

If Not searchArray Is Nothing Then
    Dim filter As String
    filter = "(&(objectCategory=group)(cn=DL*)(|"
    For j As Integer = 0 To searchArray.Length - 1
        filter = filter & "(cn=*" & searchArray(j).Trim & "*)"
    Next
    filter = filter & "))"
    MyDirectorySearcher.Filter = (filter)
Else
    MyDirectorySearcher.Filter = ("(&(objectCategory=group)(|(name=dl*)))")
End If


MyDirectorySearcher.SearchScope = SearchScope.Subtree
MyDirectorySearcher.PropertiesToLoad.Add("cn")
MyDirectorySearcher.PropertiesToLoad.Add("ManagedBy")
MyDirectorySearcher.PropertiesToLoad.Add("info")
MyDirectorySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
MyDirectorySearcher.Sort.PropertyName = "cn"

Dim MySearchResult As SearchResultCollection = MyDirectorySearcher.FindAll()

Dim myTable As New DataTable("Results")
Dim colName As String
Dim i As Integer

For Each colName In MyDirectorySearcher.PropertiesToLoad
    myTable.Columns.Add(colName, GetType(System.String))
Next

Dim result As SearchResult

For Each result In MySearchResult
    Dim dr As DataRow = myTable.NewRow()
    For Each colName In MyDirectorySearcher.PropertiesToLoad
        If result.Properties.Contains(colName) Then
            If colName = "ManagedBy" Then
                strManagedBy = CStr(result.Properties(colName)(0))
                i = strManagedBy.IndexOf(",")
                strManagedBy = strManagedBy.Substring(3, i - 3)
                dr(colName) = strManagedBy
            Else
                dr(colName) = CStr(result.Properties(colName)(0))
            End If
        Else
            dr(colName) = ""
        End If
    Next
    myTable.Rows.Add(dr)
Next
Return myTable
End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消