Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 17th, 2006, 12:55 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Cannot have multiple items selected in a DropDownL

Considering the expected behavior of drop down lists, this is the strangest error I've seen in some time. I am populating dropdown items in code, and setting the first value to the default like this:

ddList.SelectedIndex = 0

Then, when I select a different value from the browser at run time, I get this error in the SelectedIndexChanged event. I've had the same problem with radiobuttons when setting a default value in the properties window at design time. As ddls and radio buttons can only have one selected item at a time, I don't understand why selecting a different item from the selected default produces this error. I would expect it to simply deselect the previously selected value and select the new one - not throw an error. This makes no sense to me.

What do I have to do to present the user with a default item but then allow them to change the default without producing this error? How can I deselect a default value AFTER the user has chosen a different one? And I am curious WHY I should have to do this when it should happen automatically?
Old May 17th, 2006, 01:27 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts

My mistake. My code looked like this:

ddList.items(0).selected = true

But why this should cause a problem where:

ddList.selectedindex = 0

does not is beyond me. Shouldn't both lines of code produce the same results?

Hello, Microsoft?
Old May 18th, 2006, 04:59 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi Ron,

I can understand your frustration, as I ran into this a couple of times as well.

However, if you think about it from an object oriented perspective, it may make sense.

The ListItem class is just that; a simple and single list item that can be placed in a list control. Each ListItem object is able to determine whether it's currently selected or not, and you can instruct it to become selected by setting its Selected property to true.
However, once you do that, nothing else happens. One ListItem is not aware of other items in the same list, so it doesn't reset the Selected property of the other items in the list.
This is generally a good thing, because the same ListItem class can be reused in other List controls that *do* support multiple selections.

Now, if you look at the DropDownList control, things are a bit different. The DropDownList does know it can only have one item selected at all times. So when you set the SelectedIndex to, say, 4, the List will ensure that for all the other items, the Selected property is set to False. Therefore, the list is in control (no pun intended) and makes sure only one ListItem is selected.

This means that if you try to set the Selected property of a ListItem directly, you need to call ddList.ClearSelection().

Does this help?

Imar Spaanjaars
Everyone is unique, except for me.
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)

Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList, DataSource and Selected Items kingroon ASP.NET 2.0 Basics 2 February 25th, 2008 10:41 AM
Number of selected items in a listbox sarah lee ASP.NET 2.0 Basics 2 May 17th, 2007 12:20 PM
Cannot have multiple items selected in a DropDownL Ron Howerton ASP.NET 1.0 and 1.1 Basics 23 November 8th, 2006 04:29 PM
selected items in asp:ListBox jtyson Classic ASP Basics 2 July 2nd, 2004 12:01 AM
Iterating thru all child items of selected parent mahesh_ys@indiatimes.com Beginning VB 6 0 May 3rd, 2004 02:35 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.