Tuesday, August 5, 2008

ASP.NET: A DropDownList cannot have multiple items selected

For those of you looking at the title, I know that a drop down list cannot have multiple items selected. That's why we use a dropdown list - to get the user to select one item from a bunch of different values.

The problem I've been experiencing is the exception that ASP.NET throws to me when I add a blank item to the dropdown list with a:
ListItem lstBlankItem = new ListItem();
lstBlankItem.Selected = true;
ddlDropdown.Items.Add(lstBlankItem);


...and I then follow that up with a:
lstBlankItem.SelectedValue = 83;

ASP.NET doesn't automatically changed the Selected property of the ListItem added earlier to false when I set the SelectedValue to a different value. I guess it's one of those things the guys at Microsoft thought was gimmick-y ("Look, boss, we can tell when a stupid developer uses our framework to select two items in a listbox"). It's going to shoot them in the foot when some development lead finally gives in to the urge to move to Java.

I recently read that if you have multiple dropdown lists bound to the same datasource and each of the dropdown lists have different items selected, this could be the result. I've yet to try it out though.

No comments: