Listbox bug(?)

M

Mehdi

Here is what you need to do to replicate the problem that I encountered...

Drag 'n drop a ListBox control into a Webform (ASP.Net 2003/2005
application). Then add a few items into the ListBox's Items collection using
the IDE or using aspx declarative syntax. Make some of the Values the same
(but different Texts). eg,
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="2">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
here, two items have the same value.

Now, put any code you want to check the SelectedIndex property of the
Listbox. You can simply add Response.Write(ListBox1.SelectedIndex) to the
SelectedIndexChanged event handler (with AutoPostBack="True").

Run the program.

Here is what happens... when you select any of the same-valued item the
SelectedIndex returned is always that of the first item of the same-valued
items. In our example, it will return 1 whether you select Item 2 or 3.
Even more interesting, if you did turn on AutoPostBack="True", then when you
select Item 3 it will jump back to Item 2 on postback.
(However, if you are curious, javascript does return the correct
selectedIndex.)


Is this a known issue or a bug with the Listbox webcontrol?
 
T

Tasos Vogiatzoglou

There is no issue.

When you submit the form the value of the generated <select> tag is
transmited to the server and thus the server returns the index of the
first item with this value.

Javascript returns the correct index because it interfaces the actual
DOM tree.

Tasos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top