Error: Cannot have multiple items selected in a DropDownList.


Error: Cannot have multiple items selected in a DropDownList.

Description: This error occurs when we try to select a value multiple times for a DropDownList. Let say we have multiple items getting added or inserted to the control and setting a value as true during runtime multiple times. This will confuses the control and throws an exception.

DropDownList1.Items.Add(new ListItem("one", "1"));
DropDownList1.Items.Add(new ListItem("two", "2"));
DropDownList1.Items[0].Selected = true;
DropDownList1.Items.Add(new ListItem("three", "3"));
DropDownList1.Items.Add(new ListItem("four", "2"));
DropDownList1.Items[3].Selected = true;

In above example, multiple times we are trying to set a value for dropdownlist.

Solution: Using SelectedValue or SelectedIndex properties.

DropDownList1.SelectedValue = "4";
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s