Organic Search Engine Optimization (SEO) Solutions. Call now on 0879807629
AKA Marketing.com Logo

Blogged thoughts, is our web blog. Expect views, opinion, rants and tirades about everything and anything 

« Home / Services / Forums »        

Subscribe to our SEO / IT related blog by entering your email address below

Blogged thoughts

| by the www.akamarketing.com team



Cannot have multiple items selected in a DropDownList

I said I’d share this information about how to resolve this error since it took me almost a whole day to fix it. The ‘Cannot have multiple items selected in a DropDownList‘ exception will be thrown when the developer attempts to select a ListItem in a DropDownList when an item in that same DropDownList has already been selected earlier in previous code.

When searching on Google and the forums.asp.net site most of the suggested fixes to this problem suggested explicitly deselecting all selected items in a dropdown before selecting another. This can be done via something like ddl.clearselection() or ddl.SelectedIndex = -1. It seemed like a fairly logical approach, however it did not work for me, no matter what variation of deselection I tried.

On further investigation, it seemed the problem stemmed from the fact that I was adding the same ListItem to multiple dropdowns like so:

ListItem areaItem = new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString());
ddlArea.Items.Insert(i, areaItem);
ddlArea2.Items.Insert(i, areaItem);
ddlArea3.Items.Insert(i, areaItem);
ddlArea4.Items.Insert(i, areaItem);
ddlArea5.Items.Insert(i, areaItem);

but because a ListItem object is a reference object when I changed the selectedItem (and by doing so set a particular ListItems selected property to true) property of say ddlArea4 I also changed it in all the other dropdowns which contained the ListItem which was now selected. Remember reference objects do not keep a copy of an object they simply store a reference to the original object, thus a change in one dropdown list was leading to a change in all so code like the following which aims to explicitly deselect items before selecting another would not work:

ddlArea4.ClearSelection();
ddlArea4.SelectedIndex = -1;
ddlArea4.Items.FindByValue(itdp.Rows[2][1].ToString()).Selected = true; //the ListItem that this will select is present in multiple dropdowns, if any of them have anything selected an error will be thrown

The solution to the problem is to create new instances of ListItems for each option you need to add to dropdown lists, so instead of the first bit of code above, I tried something like:

ddlArea.Items.Add(new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea2.Items.Add(
new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea3.Items.Add(new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea4.Items.Add(
new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));
ddlArea5.Items.Add(new ListItem(Areas.Rows[i][0].ToString(),Areas.Rows[i][0].ToString()));

It meant when I selected a particular ListItem in a particular dropdown only the one ListItem and dropdown was affected. It worked well and it even allowed me to remove the extra deselection logic which in this case it turned out I did not need.

This was quite an uncommon scenario that led to this problem and tracking down the solution was difficult. I think in over 90% of cases the problem with the “Cannot have multiple items selected in a DropDownList” error would come from the developer trying to select more than one item in a DropDownList in a more obvious way such as simply having two or more .Selected = true statements perhaps in different parts of his or her code without realising. Therefore always give something like ClearSelection() or SelectedIndex = -1 a go first before thinking too hard about this problem, it will most likely resolve it.

9 Comments on “Cannot have multiple items selected in a DropDownList”
1| steffen said,

Thanks a lot. This is great …
I was trying to fix this prob the hole day. Creating one instance of the ListItem for each dropdownlist works for me.

Thanks :)

2| Yogesh said,

Thanks a lot,

I was trying to get rid of this problem whole day.. Really great explaination about the fact that anyone rarely know….

Thanks again..

3| Richard said,

This saved me HOURS!!! Thanks for posting.

4| Jean Wright said,

I only wish I had found this this morning. Thank you!

5| Gary said,

thanks, this worked for me and the explanation clarified the nature of the problem

6| raju said,

thankx for ur help
just add ddlArea4.ClearSelection(); as u said.

7| Lacho said,

Exactly my problem! Thanks a lot!

8| Leonardo santos-Macias said,

My 5 cents, I started to get the same error when I migrated to .net 2.0 and 3.5, my code is quite complicated but works for sure with .net 1.1. I tried this solution and still get the same error on .net 2.0 and VS2008, downgraded back to 1.1, compiled with VS2003 and works fine without code modifications

9| Gonzo said,

Actually, this issue applies to any control that uses ListItem, not just dropdowns. And it’s not that uncommon for me since my client requires us to use “Yes” and “No” dropdowns instead of checkboxes so I might have 3 or more dropdowns with the same items in the same page. Another workaround is to populate your dropdowns like this (sorry, it’s VB):

Public Sub PopulateDropDowns2(ByVal sender As DropDownList)
Dim lo As ListItem
lo = New ListItem(”Select”, -1)
sender.Items.Add(lo)
lo = New ListItem(”No”, 0)
sender.Items.Add(lo)
lo = New ListItem(”Yes”, 1)
sender.Items.Add(lo)
End Sub

Leave a Comment
Name:
Email:
Website:
 
HOME | ABOUT US | CONTACT US | SITEMAP | GOOGLE SITE SEARCH | TOP
12 Lorcan Crescent, Santry, Dublin 9, Ireland +353 87 9807629