Friday, March 30, 2012

Error: Cannot have multiple items selected in a DropDownList

Not too long ago, I created an ASP.NET website using Oracle (this is the same website as in an earlier post). On one of the administration pages, the item selected in some drop-down lists changed dynamically if items were deleted, but I was hit with the following error: Cannot have multiple items selected in a DropDownList. I was using ddlMyList.SelectedValue = someValue to change the selected item, but that wasn't working in this case. Here's how to fix this.

Problem: Website throws the error "Cannot have multiple items selected in a DropDownList" when trying to programmatically change the selected item using ddlMyList.SelectedValue = someValue.

Impact: The website is totally inaccessible when this error occurs.

Solution:
The first thing I did, of course, was to add the line ddlMyList.ClearSelection(); before I attempted to change the selection, but this still didn't work. Upon some searching, I found out that this error sometimes occurs when using the SelectedValue property to change the selected item in the list.

I overcame this problem by changing the ddlMyList.SelectedValue = someValue; line to:

ddlMyList.SelectedIndex = ddlMyList.Items.IndexOf(ddlMyList.Items.FindByValue(someValue));

Source:
System.Web.HttpException: Cannot have multiple items selected in a DropDownList, StackOverflow.

0 comments:

Post a Comment