【问题标题】:Search AD with textbox value and return results as datatable使用文本框值搜索 AD 并将结果作为数据表返回
【发布时间】:2010-09-27 15:24:36
【问题描述】:

目前我有一个绑定到数据表的网格视图,该数据表填充了来自 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

【问题讨论】:

    标签: vb.net search gridview active-directory


    【解决方案1】:

    解决方案是使用 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>
    

    搜索按钮的代码隐藏: 下面的循环在将字符串数组传递给函数之前删除所有空格

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 2023-03-06
      相关资源
      最近更新 更多