如何在数据表中分配ddl中的值? [英] How to assign values in ddl from data table ?
问题描述
ddlLocn.SelectedValue= dt.Rows[0]["LocName"].ToString();
执行语句时我需要显示所选的值/数据..
While executing the statement i need to display the selected value/data ..
Location
ddlbox
但它显示为空或显示如下错误:
ddlbox
But it shown as empty or displaying error like:
dropdownlist; has a SelectedValue which is invalid because it does not exist in the list of items
我尝试过:
我试过ddlLocn.SelectedValue = dt.Rows [0] [LocName]。ToString()
和ddllocn.selecteditem.value
以及ddllocn.text
What I have tried:
I tried ddlLocn.SelectedValue= dt.Rows[0]["LocName"].ToString()
and ddllocn.selecteditem.value
and also ddllocn.text
推荐答案
正如错误消息所示,
你需要在下拉列表中有一个元素具有与您在Selectedvalue属性中指定的值相同的值。
As the error message suggests,
You need to have an element in the dropdown which has value same as the value you are assigning to in the Selectedvalue property.
ddlLocn.SelectedValue= dt.Rows[0]["LocName"].ToString();
这里,你的下拉列表需要有一个元素,其值等于dt.Rows [0] [LocName]。ToString()
我怀疑,你忘了绑定下拉列表在您尝试使用此代码选择项目之前。
尝试绑定此行之前的下拉菜单。
希望,它有帮助:)
Here, your dropdownlist need to have a element with value which is equals to dt.Rows[0]["LocName"].ToString()
I suspect, you have forgotten to bind the dropdown before you are trying to select an item with this code.
Try binding the dropdown prior to this line.
Hope, it helps :)
试试这个
Try this
string locationName = dt.Rows[0]["LocName"].ToString();
ListItem targetItem = ddlLocn.Items.FindByText(locationName);
if (targetItem != null)
targetItem.Selected = true;
如果上述方法无效,请尝试 FindByValue [ ^ ]
这篇关于如何在数据表中分配ddl中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!