System.Web.HttpException: Cannot have multiple items selected in a DropDownList
Asked Answered
W

4

10

During page load, index 0 was already selected. Then this code statement selected index 1:

dropDownList.Items.FindByValue(myValue).Selected = true; 
// assume myValue is found at index 1 of dropDownList.Items

On completion of page load, the page reads: "System.Web.HttpException: Cannot have multiple items selected in a DropDownList."

Why did I get the exception? And how can I fix it?

Walter answered 21/10, 2009 at 20:45 Comment(2)
What part of the exception message did you not understand?Bursarial
I understood the content of the exception. However, I did not understand why it occurred. I later figured it out. See my following answerWalter
W
38

I noticed that both index 0 and index 1 had the properties "Selected" set to true (dropDownList.Items[0].Selected and dropDownList.Items[1].Selected both were true). However, dropDownList.SelectedIndex was still 0, even though index 1 was set most recently.

I tried resolving this by clearing the list selection beforehand.

dropDownList.ClearSelection();
dropDownList.Items.FindByValue(myValue).Selected = true;

But that didn't help. Same exception occurred.

What did help, was setting the selected value another way:

dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(myValue));

Now the selection change propogates throughout the list.

So, don't use dropDownList.Items[x].Selected = true/false to change the selected value of a DropDownList. Instead, use dropDownList.SelectedIndex = x;

Walter answered 21/10, 2009 at 20:51 Comment(1)
Alternatively you can use ddl.Selected = val which is an easier variation that I prefer. Also, be sure that the data is bound to your DropDownList prior to selecting the desired value. This can also cause this error by trying to select a value from a ddl which is not yet populated. I accomplished this with a simple If ddl.Items.Count < 1 Then -> ddl.DataBind(). I'm sure this isn't ideal, but it's a convenient quick fix at the least. :)Prase
C
22

I just had this problem, and found out it was caused by something different. I was adding the same ListItem instance to multiple dropdowns:

ListItem item = new ListItem("Foo", "1");
ListItem item2 = new ListItem("Bar", "2");
ddl1.Items.Add(item);
ddl2.Items.Add(item);
ddl1.Items.Add(item2);
ddl2.Items.Add(item2);

Then setting the SelectedValue:

ddl1.SelectedValue = "1"; //sets the Selected property of item
ddl2.SelectedValue = "2"; //sets the Selected property of item2

Switching to adding separate instances of ListItem fixed the problem.

My guess is that when you set the SelectedValue of the DropDownList, it sets the Selected property on the appropriate ListItem in its Items collection. So in this case, at the end of the second code block, both items are selected in both dropdowns.

Carolacarolan answered 11/2, 2012 at 0:28 Comment(5)
THIS is the scenario that got me. I was adding the same ListItem to mulitple dropdownlist controls. Got to bind a unique list item object to each unique dropdownlist control.Totter
This helped me out as well. Love stackoverflow ;-)Sundried
Thanks Jefferson,I was doing same.I got help from this.Frayda
Same here. Took me days to find this. My bad for only reading the top ranked answers. Here is my upvoteGauntry
This scenario was also concurring on my ASP.Net Web app by VB. It didn't manifest itself until farther down the road! Using the same List Item over multiple dropdowns leads to trouble. Thank you sir, well done.Interconnect
D
0

I had a similar problem but under a slightly different scenario. I thought I should post it and the resolution here as it may help someone save time if they happen to be in my similar scenario.

First the error message:

AMError: Sys.WebForms.PageRequestManagerServerErrorException:
Sys.WebForms.PageRequestManagerServerErrorException: 
Cannot have multiple items selected in a DropDownList.

My Scenario:

I was using using VisualStudio 2010 to step through the application (ASP VB Net) when I encountered the problem. I looked over the 2 dropdownlists on the page, checked the internet and wasted several hours w/o any resolution.

Resolution:

Then I got feedup and exited VS 2010 and took a break. When I came back. I reran the application and there was no problem. That's when I realized my costly mistake: I had setup an expression that set the SelectedValue in the Debugger Watch Window! Hence the multiplicity!

I removed the expression and all was well again --- Visual Studion 2010 able to get past the dropdownlist section onto the another area of focus of the application.

Donnettedonni answered 13/10, 2013 at 18:51 Comment(0)
M
0

Unfortunately Bill's answer did not work for me, as the problem is mainly in how the duplicate list is built. Also copy and pasting the code to create new list for 16 identical dropdowns is against my principles. (੭ °ཀ°)੭

What worked for me was to duplicate the dropdowns using a combination of the answers for this question and this question and then using SelectedIndex.

        var listShunt = new List<ListItem>();
        while (dr.Read())
        {
            itemShunt = new ListItem(dr["ID","TYPE"]);
            listShunt.Add(itemShunt);
        }
        numberShunt_CH3.DataSource = listShunt.ToArray();
        numberShunt_CH3.DataBind();
        numberShunt_CH3.SelectedIndex = 1;
        ...

        numberShunt_CH7.DataSource = listShunt.ToArray();
        numberShunt_CH7.DataBind();
        numberShunt_CH7.SelectedIndex = 5;
        ...
Marduk answered 17/5 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.