Multi-Select ListBox Question(s)

Posted on 2005-05-03
Last Modified: 2010-04-07
First Part is getting the values from the multi-selection listbox. (SelectionMode="Multiple")  I have tried 2 ways to get the selected values from the listbox:

               'First Attempt -> Does not return the selected items
      Dim itm As ListItem
         For Each itm In SubLi.Items
            If itm.Selected Then
               str1 &= itm.Text
            End If
               'Second Attempt -> Does not return the selected items            
      Dim ctr as Integer
                  For ctr=0 to SubLi.Items.Count()-1
                                If SubLi.items(ctr).selected
                          str1 += SubLi.items(ctr).Text
                                end if
Each of these 2 ways does not return the selected values - returns nothing.  If I access directly it returns the value str1 = SubLi.items(1).Text but I need to do it in a loop to get all of the selected values. (Assuming this is the only way to get all of the selected items from a multi-listbox)  Would be greatly appreciated if someone can point out where the logic is flawed in these loops, from what I can tell & reading some other solutions these should work.... but for some reason are not returning the selected items.

Part 2 - Set the defaults in the listbox.  How do you set multiple selected items in a list box, ie want to pre-select the first and third items?

Question by:egdigital
    LVL 12

    Accepted Solution

    Part 1:
    It seems both should work.
    I have a question: do you rebind the listbox on postback in Page_Load?
    If that is the case, your selection will be lost on postback.
    You should put your code to rebind the listbox inside
    if not ispostback then
    end if

    Author Comment

    That solved the first problem.. posting note on my laptop now to remember to place binding code inside of 'if not ispostback '.. Would you know how to address the second part of the question?
    LVL 12

    Expert Comment

    Like this?

      ListBox1.Items(0).Selected = True
            ListBox1.Items(2).Selected = True

    Author Comment

    Yes, actually I should've tried that out prior to asking.. thanks for the help & prompt answers.

    Expert Comment

    Please I need help on that

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now