Solved

How to get all the selected items in a ListBox?

Posted on 2011-09-14
5
392 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much.  That did the trick.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

10 Experts available now in Live!

Get 1:1 Help Now