Solved

How to get all the selected items in a ListBox?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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