Unexpected behaviour of Listbox MultiSelect

M

Martin

I have an unbound Listbox which is filled with ListItems. MultiSelect is set
to true. However, if the ListItems do not have unique text/value pairs, then
if a multi-selection is done, the Selected properties of the ListItems are
not set correctly.

Is this expected behaviour?

As an example, take the MSDN example at
http://msdn.microsoft.com/library/d...rfSystemWebUIWebControlsListBoxClassTopic.asp
and then change the text of the listitems, so they are not unique. The
multi-select will then not work as expected.

Or take my own example which require you to drag a Listbox and a button on a
web form. Set the selection mode to multiple and Copy/paste the following
eventhandlers to the vb page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Me.IsPostBack Then
ListBox1.Items.Add(New ListItem("one", "1"))
ListBox1.Items.Add(New ListItem("one", "1"))
ListBox1.Items.Add(New ListItem("three", "3"))
ListBox1.Items.Add(New ListItem("one", "1"))
ListBox1.Items.Add(New ListItem("one", "1"))
ListBox1.Items.Add(New ListItem("two", "2"))
ListBox1.Items.Add(New ListItem("two", "2"))
ListBox1.Items.Add(New ListItem("two", "2"))
ListBox1.Items.Add(New ListItem("three", "3"))
ListBox1.Items.Add(New ListItem("two", "2"))
ListBox1.Items.Add(New ListItem("two", "2"))
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Delete selected items
Dim SelItems As New ArrayList
Dim li As ListItem

For Each li In ListBox1.Items
If li.Selected Then SelItems.Add(li)
Next

For Each li In SelItems
ListBox1.Items.Remove(li)
Next
End Sub
 
S

Steven Cheng[MSFT]

Hi Unko,

As for the asp.net listbox multiple selection problem (when there has
duplicated key/value pairs), it is an expected behavior, not a bug of
asp.net control. This is because ASP.NET ListBox is actually a html
<select> element when rendered to clientside browser... And when we make
multiple selection, all the selected items' "value" are posted back to
server(this is the limited standard html element's behavor...). And the
asp.net listbox can only map the selected item to their index in the Items
collection by the posted "value". You can use the following code to
printout the post back value in Request.Form collection:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim key As String

For Each key In Request.Form.Keys
Response.Write("<br>" & key & ": " & Request.Form(key))
Next


So when using ListBox control with mutiple selection, we should make sure
there is no duplicated value in all the items (we can have duplicated Text
properties...)

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| Thread-Topic: Unexpected behaviour of Listbox MultiSelect
| thread-index: AcYR/NXmcvYgfq/VR5GixC2XaWLK8g==
| X-WBNR-Posting-Host: 194.60.98.5
| From: =?Utf-8?B?TWFydGlu?= <[email protected]>
| References: <[email protected]>
| Subject: RE: Unexpected behaviour of Listbox MultiSelect
| Date: Thu, 5 Jan 2006 05:35:04 -0800
| Lines: 1
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:32263
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Please can anyone from MS comment on this, as it still is an issue for me
...
|
 
M

Martin

Steven Cheng said:
.... it is an expected behavior, not a bug of
asp.net control. This is because ASP.NET ListBox is actually a html
<select> element when rendered to clientside browser...

I see where you're coming from, but don't agree with the conclusion. It's
NOT expected behaviour (nor is it a bug if you know what to expect).

MS has chosen to implement it this way. Yet a list remains a list, and if
multiple selection of items is part of the functionality, then it must work
as expected.

There's not a single word on this in the documentation! I suggest to either
update the documentation (stating explicitely that it requires unique values)
or implement the control in a different fashion, such that it works as
expected (just like in win forms).

Martin Koster
 
S

Steven Cheng[MSFT]

Thanks for your followup Martin,

I can understand your concern on this since ASP.NET encapsulate the
Dropdownlist/ListBox control, it should have encapsulated all the features
like identifying multiple selection.... However, the limitation here is
somewhat specific to the HTML <select> list element, as we knew, asp.net
ListBox or DropDownList control just render them as html <select > element,
and when using multiple selection, the client html page just post back all
the selected items (<option> )'s value to server, e.g ,

there will have a form variable like the following

one|two|one|one|two|two|three

So, we can find that no index info is contained. So if we make multiple
items have the same value, the server side can not identify which posted
entry mapped to which item (index) in the list. BTW, what will you do when
using plain html <select> list with mulitple elements with duplicated value
? (Or suppose we are using PHP, JSP , ASP 3, I think each of them will
encounter the same problem....)...

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Thread-Topic: Unexpected behaviour of Listbox MultiSelect
| thread-index: AcYf+kcaDscbUc+lSN61KasEVfUVsQ==
| X-WBNR-Posting-Host: 194.60.98.5
| From: =?Utf-8?B?TWFydGlu?= <[email protected]>
| References: <[email protected]>
<[email protected]>
<mYGi#[email protected]>
| Subject: RE: Unexpected behaviour of Listbox MultiSelect
| Date: Mon, 23 Jan 2006 00:52:02 -0800
| Lines: 18
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:32740
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| "Steven Cheng[MSFT]" wrote:
| > .... it is an expected behavior, not a bug of
| > asp.net control. This is because ASP.NET ListBox is actually a html
| > <select> element when rendered to clientside browser...
|
| I see where you're coming from, but don't agree with the conclusion. It's
| NOT expected behaviour (nor is it a bug if you know what to expect).
|
| MS has chosen to implement it this way. Yet a list remains a list, and if
| multiple selection of items is part of the functionality, then it must
work
| as expected.
|
| There's not a single word on this in the documentation! I suggest to
either
| update the documentation (stating explicitely that it requires unique
values)
| or implement the control in a different fashion, such that it works as
| expected (just like in win forms).
|
| Martin Koster
|
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top