我有一個動態創建的下拉列表控制用的SelectedValue proerty問題而無法更新:ASP .NET Dropdownlist.Selectedvalue不更新迭代槽動態創建的控件
我有一個包含一個組合和其他控件類型ctlProductosFormatos的一個自定義的控制。我在其他地方動態創建了這些自定義控件,然後我遍歷動態創建的控件中的所有組合,更新它們上的項目列表,從另一個虛擬下拉列表中複製我想要的值。
此代碼中的控件正確迭代,組合也填充正確。我的問題是,我想維護每個組合上的selectedvalue與項目更新之前的值相同,但是失敗。
如果我進入代碼並且只運行第一個組合的迭代,並且跳出循環外,這個組合是正確填充的並且具有正確的選定值,但是如果我運行完整循環,所有組合都是設置與最後一個組合相同的值,所以我想這是一些相關的東西,像我爲所有的迭代實例化相同的控件,但它似乎不是在我的代碼中。
有些小小的幫助嗎?
Dim ControlFormato As ctlProductosFormatos
' Iterates through custom controls collection, IEnumerable(Of ctlProductosFormatos)
For Each ControlFormato In Controles
' Get the dropdownlist inside the current custom control
Dim ControlComboFind As Control = ControlFormato.FindControl("cmbFotoFormato")
Dim ControlCombo As DropDownList = CType(ControlComboFind, DropDownList)
' Get the currently selected value in the dropdownlist
Dim ValorSeleccionado As String = ControlCombo.SelectedValue
' Clear the items in the current combo,and fills with the ones in a dummy combo
ControlCombo.Items.Clear()
ControlCombo.Items.AddRange(ComboPatron.Items.OfType(Of ListItem)().ToArray())
' Sets the current combo selected item with the previously saved one
ControlCombo.SelectedValue = ValorSeleccionado
Next
感謝您的提示,但問題與此無關,因爲如果我打破處理只有一個元素的循環,項目處理工作正常。我終於明白,問題出在組合複製方法中。看來這些項目是使用此方法在組合之間共享的,並且在其中一箇中更改selectedvalue會影響其他項目。我發佈瞭解決我的問題的更改。任何方式,感謝您的想法;) – tomasofen 2013-05-12 07:41:45