?
Solved

How to get all the selected items in a ListBox?

Posted on 2011-09-14
5
Medium Priority
?
442 Views
Last Modified: 2012-05-12
I'm having trouble getting all of the selcted items of a ListBox.

    <asp:ListBox ID="txtPrograms" runat="server" Rows="6" SelectionMode="Multiple" Width="350px" >
    	<asp:ListItem Value="REA" />
    	<asp:ListItem Value="TAA" />
    	<asp:ListItem Value="WIA" />
    	<asp:ListItem Value="WP" />
    	<asp:ListItem Value="WTP" />
    	<asp:ListItem Value="PPN" />
    </asp:ListBox>

Open in new window

I've tried several variations and I always only get the first selected item.  
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    
        Dim Programs As String = ""
        If txtPrograms.SelectedIndex > -1 Then
            Programs = ""
            Dim ctr As Integer
            For ctr = 0 To txtPrograms.Items.Count() - 1
                If txtPrograms.Items(ctr).Selected Then
                    Programs += txtPrograms.Items(ctr).Text & "<br />"
                End If
                txtPrograms.Text = Programs
            Next ctr
        End If

		Call SendEmail() 'Sends txtPrograms.Text in the body of an email.
	End Sub

Open in new window

0
Comment
Question by:megnin
  • 3
5 Comments
 
LVL 12

Expert Comment

by:viralypatel
ID: 36537593
Normally you'll need to loop through and check; using LINQ it is a one line code.
Samples below:
// Items collection
foreach (ListItem item in ListBox1.Items)
{
    if (item.Selected)
    {
        // item ...
    }
}

// LINQ over Items collection (must cast Items)
var query = from ListItem item in ListBox1.Items where item.Selected select item;
foreach (ListItem item in query)
{
    // item ...
}

// LINQ lambda syntax
var query = ListBox1.Items.Cast<ListItem>().Where(item => item.Selected);

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 36537597
Please try the following:

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    
        Dim Programs As String = ""
        If txtPrograms.SelectedIndex > -1 Then
            Programs = ""
            Dim ctr As Integer
            For Each idx In txtPrograms.GetSelectedIndices()
                    Programs += txtPrograms.Items(idx).Text & "<br />"
            Next ctr
            txtPrograms.Text = Programs
        End If

		Call SendEmail() 'Sends txtPrograms.Text in the body of an email.
	End Sub

Open in new window

0
 
LVL 1

Author Comment

by:megnin
ID: 36537679
Great!

I did have to move the variable "Program" out to the Class level and then send the value of "Programs" instead of the value of txtPrograms.Text to get all the items, but it works.

        If txtPrograms.SelectedIndex > -1 Then
            Programs = ""
            Dim idx As Integer
            For Each idx In txtPrograms.GetSelectedIndices()
                    Programs += txtPrograms.Items(idx).Text & "<br />"
            Next idx
            txtPrograms.Text = Programs
        End If

Open in new window


I probably don't need " txtPrograms.Text = Programs" either.
0
 
LVL 1

Author Comment

by:megnin
ID: 36537712
I also added
If Page.IsPostback
to keep it from sending me an email when the page first loads.  ;-)
0
 
LVL 1

Author Closing Comment

by:megnin
ID: 36538222
Thank you very much.  That did the trick.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

840 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