Solved

How to get all the selected items in a ListBox?

Posted on 2011-09-14
5
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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