Reputation: 269
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
Reputation: 17614
You can use like this
DropdownList1.SelectedIndex =
DropdownList1.Items.IndexOf(DropdownList1.Items.FindByValue(strText));
References
Setting dropdownlist selecteditem programmatically
Upvotes: 0
Reputation: 148140
You can use DropDownList.Items.FindByValue
if( ItemCollection.FindByValue(strText) != null)
{
DropdownList1.SelectedValue = strText;
}
Upvotes: 3