Will_G
Will_G

Reputation: 269

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?

Upvotes: 3

Views: 150

Answers (2)

शेखर
शेखर

Reputation: 17614

You can use like this

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

References
Setting dropdownlist selecteditem programmatically

Upvotes: 0

Adil
Adil

Reputation: 148140

You can use DropDownList.Items.FindByValue

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

Upvotes: 3

Related Questions