Bug dropdownlists???

Kurt

Regular
Joined
Feb 14, 2003
Messages
99
Location
Copenhagen
Hi,

I was doing something with a dropdownlist, but - at least in my eyes - weird things happen. Whenever a user makes a selection from a dropdownlist, the listindex property of the dropdownlist is set to the LOWEST listindex with the same 'item-value' as the selection made. What I mean is the folowing... Suppose you have a dropdownlist with folowing items;

Code:
text: item1 (value: 10)
text: item2 (value: 20)
text: item3 (value: 30)
text: item4 (value: 40)
text: item5 (value: 50)
text: item6 (value: 20)

Whenever a user selects item6, in the code behind the SelectedIndex property of the DropDownList reports 1, so the code behind is informed that item2 was choosen!!!

I know that in HTML, values should be unique and that propably gives an explanation of what's going on. But in the Microsoft documentation under System.Web.UI.WebControls.ListItem you will find that listitems can be used as folows;
For example, you can display text for an item in the list control, such as "Item 1", and use the Value property to specify a value for that item, such as "$1.99".
But as stated, this would for a commercial website mean that when a customer want's to buy for example item10 that by coincidence also got the value "$1.99", item 1 would be ordered for him in stead...
 
Kurt said:
I know that in HTML, values should be unique and that propably gives an explanation of what's going on. But in the Microsoft documentation under System.Web.UI.WebControls.ListItem you will find that listitems can be used as folows;

For example, you can display text for an item in the list control, such as "Item 1", and use the Value property to specify a value for that item, such as "$1.99".

But as stated, this would for a commercial website mean that when a customer want's to buy for example item10 that by coincidence also got the value "$1.99", item 1 would be ordered for him in stead...

The MS example doesn't imply that values for dropdownlist options shouldn't be unique - I think it shows that values don't have to be integer/long values (the values could be any type of string value that might also happen to be an integer, decimal, etc).

An ASP:DropDownList is rendered as an html select element containing html option elements - and as you stated - you'd want to have unique values if you wanted to be sure of what text was associated with a given value.

I can think of a way around this issue if you were so inclined. You could put a hidden input on your webform (with runat=server so it could be handled in codebehind) and stick a javascript onchange event on your dropdownlist such that whenever it changes the hidden input is updated with the selectedIndex of the dropdownlist (html select element). Then when you postback you could examine the value of the hidden input to know exactly which list item was selected. You would identify the true 'selected index' using hidden input's value as an index of for the items collection of the dropdownlist - instead of using the dropdownlists 'SelectedIndex' property which has to guess when there are duplicate values.

Good luck,
Paul
 
Back
Top