【问题标题】:How do I populate manager dropdownlist based on value of employee dropdownlist?如何根据员工下拉列表的值填充经理下拉列表?
【发布时间】:2013-02-13 15:12:51
【问题描述】:

现在,我正在尝试提出 2 个 DropDownLists,一个是员工 DropDownList,另一个是经理 DropDownList

首先,我不熟悉 Active Directory 的工作原理,但在进行了一些研究后,我确实发现了类似下面的代码,据我了解,它代表了管理器的定义:

   Dim deEmployee As New DirectoryEntry("LDAP://CN=John Employee,OU=Sales,DC=Corp,DC=com")

   deEmployee.[Property]("manager") = "CN=Peter Manager,OU=Sales,DC=Corp,DC=com"
   deEmployee.CommitChanges()

由于有几个经理,我可以像上面那样硬编码名称 = CN=Peter Manager

我可以使用哪个组代表 Active Directory 中的经理,而不是 CN=Peter Manager

但对我来说更大的问题是,如果我从第一个 DropDownList 中选择员工,它如何填充第二个 DropDownList 与该员工的经理?

据我了解,部门是将员工与经理联系起来的属性,但我如何在代码中使用它?

在正常的级联下拉列表中,我可以选择员工并在第一个下拉列表中列出员工所属的部门,在第二个下拉列表中,我可以选择经理 where department = theDepartmentListed in first dropdownlist。

这是查询数据库,但在我的例子中,我们查询的是 Active Directory。

有人可以说明如何根据部门将第一个DropDownList中的员工和第二个DropDownList中的经理之间的关系联系起来吗?

【问题讨论】:

    标签: asp.net vb.net active-directory


    【解决方案1】:

    AD 中没有内置的管理员组,您可以直接从组或 OU 中查询他们的唯一方法是您的组织添加了一个。

    没有自动将员工链接到经理,因此您必须查询部门并选择正确的用户作为经理。

    您将需要编写一个查询来获取除所选用户之外的部门中的所有用户,这样应该可以工作:

    Imports System.DirectoryServices
    
    ...
    
    Protected Sub EmployeeChanged(sender as object, e as EventArgs) Handles ddlEmployees.SelectedIndexChanged
    
        Dim selectedUser as new DirectoryEntry(ddlEmployees.SelectedValue) 'assuming your Value on the empoyees dropdown is the LDAP object path
        Dim domainRoot as new DirectoryEntry("LDAP://DC=corp,DC=com")
        Dim searcher as new DirectorySearcher()
        searcher.SearchRoot = domainRoot 
        searcher.Filter = "(&(objectClass=user)(department=" & selectedUser.Properties("department").Value & "))"
    
        Dim results as SearchResultCollection = searcher.FindAll()
    
        For Each result as SearchResult in results
    
            Dim de as DirectoryEntry = result.GetDirectoryEntry()
    
            If de IsNot Nothing Then
    
                If Not de.Properties("samaccountname").Value.ToString().ToLower() = selectedUser.Properties("samaccountname").Value.ToString().ToLower() Then
    
                    ddlManagers.Items.Add(de.Properties("displayName").Value.ToString(), de.Properties("distinguishedName").Value.ToString())
    
                End If
    
            End If
    
        Next
    
    End Sub
    

    有关编写 LDAP 查询的更多信息:http://technet.microsoft.com/en-us/library/aa996205(v=exchg.65).aspx

    【讨论】:

    • 非常感谢。我遇到了以下错误:overload resolution failed because no accessible 'Add' accepts this number of arguments 在以下行:ddlManagers.Items.Add(de.Properties("displayName").Value.ToString(), de.Properties("distinguishedName").Value.ToString())。我似乎无法弄清楚。
    • 这很奇怪...尝试将其更改为Dim newItem as new ListItem(de.Properties("displayName").Value.ToString(), de.Properties("distinguishedName").Value.ToString()) ddlManagers.Items.Add(newItem)
    • 谢谢@Sean。现在,我得到了这个:Value of type 'System.DirectoryServices.SearchResult' cannot be converted to 'System.DirectoryServices.DirectoryEntry'. 在这条线上:Dim de As DirectoryEntry = CType(result, DirectoryEntry)
    • 我将结果替换为发件人,例如:Dim de As DirectoryEntry = CType(Sender, DirectoryEntry) 我不知道这是否是解决方案,但错误消失了。
    • 不,这不是解决方案,这是我的错误,我已编辑答案以反映 =]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多