Search code examples
asp.neterror-handlingselectedvalue

catch a error: is not in list


how can I catch this error?

DropdownList1.SelectedValue = strText;  

This is the error:

Selected Value of DropdownList1 is invalid because it does not appear in the list of items.

Can I catch it with something like strText exist in dropdownlist1?


Solution

  • You can use DropDownList.Items.FindByValue

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