发现错误:不在列表中

时间:2013-03-19 08:58:13

标签: asp.net error-handling selectedvalue

我怎样才能发现这个错误?

DropdownList1.SelectedValue = strText;  

这是错误:

  

DropdownList1的选定值无效,因为它没有出现在项目列表中。

我可以在dropdownlist1中找到类似strText的内容吗?

2 个答案:

答案 0 :(得分:3)

您可以使用DropDownList.Items.FindByValue

if( ItemCollection.FindByValue(strText) != null)
{
    DropdownList1.SelectedValue = strText;
}

答案 1 :(得分:0)

你可以这样使用

 DropdownList1.SelectedIndex =    
                  DropdownList1.Items.IndexOf(DropdownList1.Items.FindByValue(strText));

参考文献
Setting dropdownlist selecteditem programmatically