Unexpected behaviour of Listbox MultiSelect

Discussion in 'ASP .Net Web Controls' started by Martin, Oct 18, 2005.

  1. Martin

    Martin Guest

    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
     
    Martin, Oct 18, 2005
    #1
    1. Advertising

  2. Martin

    Martin Guest

    Please can anyone from MS comment on this, as it still is an issue for me ...
     
    Martin, Jan 5, 2006
    #2
    1. Advertising

  3. Restate the issue and maybe someone can take a look into it.
    --
    Christopher A. Reed
    "The oxen are slow, but the earth is patient."

    "Martin" <> wrote in message
    news:...
    > Please can anyone from MS comment on this, as it still is an issue for me
    > ...
     
    Christopher Reed, Jan 6, 2006
    #3
  4. 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?= <>
    | References: <>
    | Subject: RE: Unexpected behaviour of Listbox MultiSelect
    | Date: Thu, 5 Jan 2006 05:35:04 -0800
    | Lines: 1
    | Message-ID: <>
    | 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
    ...
    |
     
    Steven Cheng[MSFT], Jan 9, 2006
    #4
  5. Martin

    Martin Guest

    "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
     
    Martin, Jan 23, 2006
    #5
  6. 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?= <>
    | References: <>
    <>
    <mYGi#>
    | Subject: RE: Unexpected behaviour of Listbox MultiSelect
    | Date: Mon, 23 Jan 2006 00:52:02 -0800
    | Lines: 18
    | Message-ID: <>
    | 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
    |
     
    Steven Cheng[MSFT], Jan 24, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lenard Gunda
    Replies:
    3
    Views:
    1,276
    =?Utf-8?B?S2FubmFuLlYgW01DU0QubmV0XQ==?=
    Aug 17, 2005
  2. Jeff Thur
    Replies:
    2
    Views:
    211
    Kostadin Kostov
    Feb 14, 2005
  3. Nick Nelson

    Listbox Multiselect as filter for Gridview

    Nick Nelson, Feb 28, 2007, in forum: ASP .Net Web Controls
    Replies:
    8
    Views:
    1,279
    chiefsss
    Aug 24, 2009
  4. Jay Pondy

    ObjectDataSource / MultiSelect ListBox

    Jay Pondy, Sep 22, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    526
    Jay Pondy
    Sep 22, 2007
  5. Kelly McDonald

    multiselect click behaviour mod in IE.

    Kelly McDonald, Nov 4, 2003, in forum: Javascript
    Replies:
    0
    Views:
    113
    Kelly McDonald
    Nov 4, 2003
Loading...

Share This Page