Question

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?

Was it helpful?

Solution

You can use DropDownList.Items.FindByValue

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

OTHER TIPS

You can use like this

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

References
Setting dropdownlist selecteditem programmatically

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top