Blog Stats
  • Posts - 15
  • Articles - 2
  • Comments - 22
  • Trackbacks - 19

 

The interesting DropDownList ASP.NET control behavior

So if you've decided to use syntax such as this

myDropDownListBox.Items[0].Selected = true;

or

ListItem foo = myDropDownListBox.Items.FindByValue(”foo”);
foo.Selected = true;

you will get the 'A DropDownList cannot have multiple items selected' error.

Seems to me that if the code sets the Selected attribute on an item in a list which by definition can only have one item selected, that the ListItem instance would set the SelectedIndex automatically for me. Seems like a no-brainer. I guess Microsoft found some sort of use for this error being thrown from a DropDownList control - just don't ask me what that could be.

I guess I'd rather see that the DropDownList have DropDownList items if the Selected property was pretty much useless and will only cause errors if used in a DropDownList.


Feedback

# re: The interesting DropDownList ASP.NET control behavior

Gravatar It could be a help to anyone who didn't know that a DropDownList did not allow multiple items selected?

The error should be there, in case someone tried selecting more than one item:

myDropDownListBox.Items[0].Selected = true;
myDropDownListBox.Items[1].Selected = true;

This way, if the SelectedIndex was reset for every new selection, no error would appear, thus leaving the developer confused: "I selected two items programmatically, but only one item ends up actually being selected. What am I doing wrong?" 9/26/2005 11:01 PM | Jannik Anker

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Then I challenge why there isn't a DropDownListItem class and then ListItem inherits from it adding the Selected property as opposed to having it available as a member of Items in a DropDownList control. 9/27/2005 9:41 AM | Allen Guest

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Here's how I set the selected index (in VB):

ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("foo")) 1/31/2006 9:43 AM | John Reiner

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Thanks for the post, but I was merely indicating my dissatisfaction with the implementation of the asp.net drop down list control. The drop down list control by nature cannot have more than one item selected so why force us to indicate that in our code?

For that matter, the whole POST mentality for ASP.NET is it's greatest flaw, mired performance and processing issues.

imho, AJAX is a far better approach to take. You can design .NET controls if you want to get the design time functionality out of them, but make them AJAX aware and please for the love of God don't rely on ViewState and Postbacks. Poor excuse for a development paradigm.

I've implemented AJAX on several major projects since abandoning the .NET postback approach and have been very pleased with the results. 2/1/2006 10:15 AM | Allen

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Thanks guys... I've solved my drop down problem by seeing your discussions... Thanks a lot... Thank you soo much... 9/5/2006 10:04 PM | Arunagiri GT

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Dim item As New ListItem
item.Text = "All"
item.Value = "%"
dlSelector.Items.Add(item)

The above code caused problems. For some reason, it seems that it would be selected
and when I would try to set the selected index to another value it would try to
select both items. I replaced the code with the following and the problem went away.

dlSelector.Items.Add(New ListItem("All", "%"))

I don't know why it is different, but somehow it is. 9/28/2006 5:50 AM | John.Net

# When i am using the dropdownlist control in webform.if i select the value from the list,how to refresh the value.

Gravatar Pleas reply me. 10/8/2007 4:29 AM | senthil

# re: The interesting DropDownList ASP.NET control behavior

Gravatar The above poster is correct.

If you add items to a dropdown like this:

dlSelector.Items.Add(New ListItem("All", "%"))

You don't have the multiple selected items problem.

Go figure. 2/26/2008 3:46 PM | James

# another one that causes it...

Gravatar This can also be caused by programmatically creating a New ListItem and adding it to two separated DropDownLists.

I thought applying to different DDL's would just add a "copy" of the New ListItem, but it doesn't....those stay connected permenantly.

For i As Integer = 0 To 18 NewListItem = New ListItem("test", "a")
ddlOne.Items.Add(NewListItem)
ddlTwo.Items.Add(NewListItem)
Next

The above code causes issues, that result in the "cannot have multiple selected items" error.

2/27/2008 5:05 PM | Brandon

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Thank you so much John.Net, I spent several days trying to figure out what the problem is.

I have two drop down lists with the same data, I fill them with same oracle data reader. Now, for filling I used the following:

Dim li as new ListItem(oracleReader("Text"),oracleReader("Value"))

and I add the "li" object to both the lists

ddl1.Items.Add(li)
dd2.Items.Add(li)

dd2.Items.Add(li)

now, if I put SelectedIndex of dd2, I get the error "Cannot Have Multiple Items Selected" error

But, if I use like

dd1.Items.Add(new ListItem(oracleReader("Text"),oracleReader("Value")))

dd2.Items.Add(new ListItem(oracleReader("Text"),oracleReader("Value")))

and then do

dd2.selectedIndex = 0, it runs fine.

Thank you so much once again.

3/17/2008 8:00 AM | Noorul Ahmed

# re: The interesting DropDownList ASP.NET control behavior

Gravatar The item is added to the dropdown lists by reference, so when you have this in mind this behaviour should not come as a surprise.

/lasse 3/18/2008 8:22 AM | Lasse Schioettz

# re: The interesting DropDownList ASP.NET control behavior

Gravatar First we need to clear the previous selection. like
ddl.ClearSelection();
ddl.Items.FindByValue("foo").Selected = true; 3/19/2008 12:47 AM | Raju E

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Thanks for that, I have found that the behaviour is even stranger, if you use

dim li as new listitem
li.value="s"
li.value="Something"
ddl.items.add(li)
li.value="w"
li.value="wierd"
ddl.items.add(li)

then the last item added becomes all the items in the list.

the add(new listitem...

sorts this out

:o) 4/14/2008 4:53 PM | Richard

# re: The interesting DropDownList ASP.NET control behavior

Gravatar "It could be a help to anyone who didn't know that a DropDownList did not allow multiple items selected?"

If you don't understand this IMHO you really should not be a programmer. The dropdown list widget is one of many widgets one could use in an application.

If one does not have the mental capacity to select the correct widget fpr the job then maybe they should be thinking abot going back to school?

/with respect 6/24/2008 9:29 AM | zoe

# re: The interesting DropDownList ASP.NET control behavior

Gravatar ^ please excuse my typos, it's been a long day. :] 6/24/2008 9:33 AM | zoe

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Dear Zoe,

I don't think that the issue is that this person does not understand what is going on, the issue is WHY is it this way? Multiple - select list boxes are nothing new to people in the Win32 world. It just appears that the code behind model isn't consistent and although it allows you to code for multi-select, no indication / errors or otherwise is given when it doesn't behave as programmed.

There are numerous examples of Microsoft being inconsistent with their implementation, this is simply another one. Knowing about the behavior of some of their implementations is key to using them properly.

I think your comment was short sighted and not warranted. Indicating someone should go back to school for not understanding WHY some developer at Microsoft implemented the behavior in that manner is no indication of mental capacity. It is important to question design and implementation in all code you re-use, 3rd party or not, so that you can better improve your own code. To blindly use code and not question or examine its design is a clear indication of a lack of imagination and developer expertise IMHO.

/with respect 7/14/2008 10:02 AM | Allen Guest

# re: The interesting DropDownList ASP.NET control behavior

Gravatar good helpfull 3/3/2009 5:21 AM | rohit.guru2004

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Wow, I never knew that interesting DropDownList. That's pretty interesting...
9/16/2009 1:29 AM | Yachtcharter Griechenland

# re: The interesting DropDownList ASP.NET control behavior

Gravatar Thanks for a sharing this articles.
11/7/2009 2:15 AM | Yachtcharter Griechenland

Post a comment





 

 

 

Copyright © Allen Guest