Far Away Developer

Sebastien Lachance

Cannot have multiple items selected in a DropDownList.

Posted by Sebastien Lachance on March 8, 2008

Cannot have multiple items selected in a DropDownList.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Cannot have multiple items selected in a DropDownList.
Source Error:

Line 253:            total = parseFloat(lblTotal.innerHTML.replace('$', '').replace(',', '.'));
Line 254:            fBudget = parseFloat(budget.replace('$', '').replace(',', '.'));
Line 255:            var lblWarningContract = document.getElementById('<%= lblWarningContract.ClientID %>');
Line 256:
Line 257:            if (total > fBudget)

I spent at least one hour figuring this out. The Source Error  does not contain any valid information. The solution is : you should never use the same item twice on two different dropdownlist.

ListItem item = new ListItem(" -- Select one -- ", "-1");

ddlLoadingContact.Items.Insert(0, item);
ddlUnloadingContact.Items.Insert(0, item);

A ListItem have a property called Selected that tells which one (the item) the dropdownlist should display as selected. If you use the same ListItem in more that one dropdownlist and you select this item in the DropDownA and a different item in DropDownB. The DropDownB will have two ListItem with the Selected property set to true. Since this is the same item in both dropdownlist.

ddlLoadingContact.SelectedValue = "-1"; //item will have the Selected property set to true
ddlUnloadingContact.SelectedValue = "John";  //another item will have the Selected property set to true. And both are contained in this dropdownlist.
 
 

4 Responses to “Cannot have multiple items selected in a DropDownList.”

  1. Leepa Says:

    thanks a LOT!
    I’ve spent whole day debugging and google’ing without success.

  2. Sebastien Lachance Says:

    You are welcome!

  3. Ramya Says:

    Thanks for the post it was very useful

  4. Angela Says:

    it’s very helpful, THX a lot!!!!!!!!
    I spent a day wonder around…………

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>