I am looking for a little help with my project here. In my code I have two drop down lists. The first list is populated on page load with results coming from an LDAP query.. So that drop down works good and presents the user with a list of active users in AD.
Now here's my head scratcher... I have a formview control in my project. I have placed an additional drop down list in the edititem template. What I want to do is copy the contents of the first drop down into the second one. The problem I'm having is that when I try to write code for the second, I keep getting an error that the second control hasn't been declared yet, which actually makes sense to me because the form doesn't really exist on the page until someone selects an index in the gridview control that the formview is tied to.
I'm thinking that in order to do this, I need to use the .findcontrol operator on the formview's edititem event. However, when I tried this I still got errors about the control's ID not being declared. I'll paste what I've tried so far here..
'' Here's where I populate that first dropdown list in code behind
Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP:MyIPaddress", "MyDomain/Username", "MyPassword", AuthenticationTypes.FastBind)
Dim searcher As DirectorySearcher = New DirectorySearcher(dirEntry)
'' Filter the search so that it only pulls up Active user accounts. The search filter "!userAccountControl:1.2.840.113556.1.4.803:=2" removes
'' disabled users from the list. For a list of all attributes defined by Active Directory, see https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx
searcher.Filter = "(&(objectClass=User)(objectCategory=person)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
'' Loop through the search results and add each user as string types to list controls
Dim userNames As New List(Of String)
For Each resEnt As SearchResult In searcher.FindAll()
Dim userName As String = resEnt.Properties("name")(0).ToString()
userNames.Add(userName)
Next
userNames.Sort()
For Each userName In userNames
userList.Items.Add(userName)
Next
'' Here's what I have so far in my attempts to find the second control...
Protected Sub FormView1_DataBound(sender As Object, e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
DropDownList(userListEdit = FormView1.FindControl("userListEdit"))
End If
End Sub
'' As soon as I try to write the last line to find the control, I get an error back saying the control isn't declared. I have tried to find a solution in many different forums online. But it seems nothing is giving me precisely what I need. Does anyone know how I can find this control and copy the contents in code behind? Thanks in advance!
From what it looks like, the answer may be simple. Your code is:
DropDownList(userListEdit = FormView1.FindControl("userListEdit"))
What you need to do is:
Dim ddlList As DropDownList = FormView1.FindControl("userListEdit")
Or you can DirectCast() and access directly:
DirectCast(FormView1.FindControl("userListEdit"), DropDownList) //.anyMethod()