从 WebDataGrid 填充 DropDownList
我有一个链接到 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 。这会导致问题吗?有没有办法解决这个问题?
I have a WebDataGrid linked to a SqlDataSource to display data. When a row is selected, I want to populate some textboxes and dropdownlists with the data from that row, for editing purposes. The textboxes work fine; I use the following code to select the second field in the grid (Date) and populate the corresponding textbox (I'm using VB):
txtDate.Text = currentRow.Items(2).Value
However, when I try to populate the dropdownlists using the same code structure, I get the following error:
'ddlType' has a SelectedValue which is invalid because it does not exist in the list of items.
I think the problem may be that the dropdownlists are populated using type_id, while the grid displays type_name. Would this be causing the problem, and is there a way to get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以下几种操作:
更改 SQLDataSource 以同时返回 type_id 并将该列设置为visible="false",这样您就可以选择该字段而不是名称并将其用于 .SelectedValue。
如果这是不可能的,那么您可以使用这样的代码(只有当下拉列表中显示的文本是唯一的并且与网格视图中显示的文本完全相同时,这才有效):
'将其替换为对 currentRow.Items(#).Value 的调用
将某些文本变暗为字符串=“type_name”
DropDownListName.Items.FindByText(sometext).Selected = True
另一种可能性是循环遍历下拉列表中的每个元素并找到文本,这就像暴力方法但有效。
'将其替换为对 currentRow.Items(#).Value 的调用
将某些文本调暗为字符串 =“type_name”
对于 ddArriveAMPM.Items 中的每个 ddItem 作为 ListItem
There are couple of things you could try:
Change the SQLDataSource to return the type_id also and set the column to visible="false" that way you could select that field instead of the name and use that for .SelectedValue.
If that is not possible then you could use code like this (this will only work if the text that is displayed in the drop down is unique and is exactly the same as what is displayed in the grid view):
'replace this with the call to currentRow.Items(#).Value
Dim sometext As String = "type_name"
DropDownListName.Items.FindByText(sometext).Selected = True
The other possibility is to loop through each element in the drop down and find the text, this is like the brute force method but works.
'replace this with the call to currentRow.Items(#).Value
Dim sometext As String = "type_name"
For Each ddItem As ListItem In ddArriveAMPM.Items
您必须先将该项目添加到列表框中,然后才能选择它。
或
确保在选择新行时清除下拉框,否则旧行值将保留在其中。
之后您可以使用 SelectedValue
Cheers,
斯特凡
You have to add the item to the listbox before you can select it.
or
Make sure to clear the DropDown box when selecting a new row otherwise the old rows values will stay in there.
After this you can use SelectedValue
Cheers,
Stefan