我有一个链接到SqlDataSource的WebDataGrid来显示数据。当一行被选中时,我想用该行中的数据填充一些文本框和下拉列表,以便进行编辑。文本框运行良好;我使用以下代码选择网格中的第二个字段(日期)并填充相应的文本框(我使用的是VB):
txtDate.Text = currentRow.Items(2).Value但是,当我尝试使用相同的代码结构填充下拉列表时,我得到了以下错误:
'ddlType' has a SelectedValue which is invalid because it does not exist in the list of items.我认为问题可能是下拉列表是使用type_id填充的,而网格显示的是type_name。这会导致这个问题吗?有没有办法绕过这个问题?
发布于 2010-12-17 00:49:02
您可以尝试以下几种方法:
'replace this with the call to currentRow.Items(#).Value
Dim sometext As String = "type_name"
DropDownListName.Items.FindByText(sometext).Selected = True
'replace this with the call to currentRow.Items(#).Value
Dim sometext As String = "type_name"
For Each ddItem As ListItem In ddArriveAMPM.Items
Next的If String.Compare(sometext, ddItem.Text, True) = 1 Then ddItem.Selected = True End If
发布于 2010-12-17 00:00:05
您必须先将项目添加到列表框中,然后才能选择它。
DropDownListX.Items.Add("Item");或
DropDownListX.Items.Add(new ListItem("String","Value"));确保在选择新行时清除DropDown框,否则旧的行值将保留在那里。
DropDownListX.Items.Clear();在此之后,可以使用SelectedValue
干杯,斯特凡
https://stackoverflow.com/questions/4462490
复制相似问题