Using the syntax ‘DropDownList1.SelectedIndex = 10;’ does not create an exception, but using the syntax ‘DropDownList.Items.FindByValue(“10”).Selected = true;’ can create the following exception:
Cannot have multiple items selected in a DropDownList. at System.Web.UI.WebControls.DropDownList.VerifyMultiSelect()
Calling ClearSelection() on the DropDownList first will avoid this exception. It is also a good idea to first check and make sure that the value actually exists as an item within the DropDownList as in the following example:
private void SetDropDownListSelectedItem(string selectedValue)
{
ListItem li = DropDownList1.Items.FindByValue(selectedValue);
if (li != null)
{
DropDownList1.ClearSelection();
li.Selected = true;
}
}