2016-07-21 20 views
1

我期待與我在這裏的項目幫助不大下拉列表。 在我的代碼中,我有兩個下拉列表。第一個列表中填充在頁面加載與結果從LDAP查詢來..所以說下拉運作良好,並呈現活躍用戶的AD列表中的用戶。ASP.NET Visual Basic代碼後面..試圖填充駐留在FormView控件

現在,這裏是我摸不到頭腦...... 我已經在我的項目FormView控件。我在edititem模板中添加了一個額外的下拉列表。我想要做的是將第一個下拉菜單的內容複製到第二個下拉菜單中。我遇到的問題是,當我嘗試編寫代碼的第二次,我一直因爲形式並沒有真正的存在讓這實際上對我來說很有意義,第二控制未聲明的是,錯誤頁面,直到有人在formview綁定的gridview控件中選擇一個索引。

我想,爲了做到這一點,我需要使用.findcontrol操作上FormView的edititem事件。但是,當我嘗試這個時,我仍然有錯誤的控件的ID沒有被宣佈。我會貼上什麼我試過到目前爲止在這裏..

「」這裏就是我填充在代碼第一個下拉列表後面

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 

「」這裏是我到目前爲止,我嘗試找到第二個控制...

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 

「」當我嘗試寫的最後一行找到了控制,我得到一個錯誤回話說控制沒有聲明。我試圖在許多不同的論壇上在線找到解決方案。但似乎沒有什麼能給我準確的我需要的東西。有誰知道我可以如何找到這個控件並將代碼中的內容複製到後面?提前致謝!

回答

0

從什麼樣子,答案可能很簡單。您的代碼是:

DropDownList(userListEdit = FormView1.FindControl("userListEdit")) 

你需要做的是:

Dim ddlList As DropDownList = FormView1.FindControl("userListEdit") 

或者你可以DirectCast()和直接訪問:

DirectCast(FormView1.FindControl("userListEdit"), DropDownList) //.anyMethod() 
0

@peyote男孩 太謝謝你了爲你的答案。這給我看了正確的語法..從那裏我不得不學習一些關於會話狀態的信息,以獲得我想要的結果。

在我的網頁加載事件,我加入這行代碼

Session("userNames") = userNames 

這使得在當前會話狀態的列表,以便以後可以使用它。然後我用你的建議終於得到我期待的結果。

Protected Sub DetailsView1_Databound(sender As Object, e As System.EventArgs) Handles DetailsView1.DataBound 
If DetailsView1.CurrentMode = DetailsViewMode.Edit Then 
    Dim ddlList As New DropDownList 
    ddlList = DetailsView1.FindControl("userEditList") 
     For Each userName In Session("userNames") 
      ddlList.Items.Add(userName) 
     Next 
     End If 
     End Sub 

再次感謝您的幫助! :)