3

我怎样才能捕捉到这个错误?

DropdownList1.SelectedValue = strText;  

这是错误:

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

我可以用 dropdownlist1 中的 strText 之类的东西来捕捉它吗?

4

2 回答 2

3

您可以使用DropDownList.Items.FindByValue

if( ItemCollection.FindByValue(strText) != null)
{
    DropdownList1.SelectedValue = strText;
}
于 2013-03-19T09:00:12.043 回答
0

你可以这样使用

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

以编程方式
设置下拉列表选定项

于 2013-03-19T09:06:03.220 回答