使用文本框值搜索AD并将结果作为数据表返回

时间:2010-09-27 15:24:36

标签: vb.net search gridview active-directory

目前我有一个gridview绑定到一个数据表,该数据表填充了AD中的组。我需要能够添加搜索功能,以便用户可以键入组名的一部分,并使结果仅显示符合其搜索条件的组。这是我到目前为止所拥有的。

<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:ObjectDataSource>

NewEmployee类:

    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

1 个答案:

答案 0 :(得分:0)

解决方案是使用FormParameter控件传递文本字段的值,我必须设置在函数中用作变量名的名称(searchArray)属性:

<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: 下面的循环在将字符串数组传递给函数

之前删除所有空格
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

NewEmployee类:

 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