【发布时间】:2013-05-12 00:52:21
【问题描述】:
动态创建的下拉列表控件中的 selectedvalue 属性没有更新:
我有一个 ctlProductosFormatos 类型的自定义控件,其中包含一个组合和其他控件。我在其他地方动态创建了这个自定义控件,然后遍历动态创建的控件中的所有组合,以使用我想要的值从另一个虚拟下拉列表中复制它们上的项目列表。
这段代码中的控件是右迭代的,combo也是右填充的。我的问题是我想将每个组合上的 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
【问题讨论】:
标签: .net dynamic drop-down-menu selectedvalue