Solved

How to get all the selected items in a ListBox?

Posted on 2011-09-14
5
417 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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