How to Get All Selected Items of a ListBox in Asp.Net C# VB.Net

In previous articles I have explained how to Bind Data into a ListBox Control. Here in this article I will describe how one can Read or Get all Selected Items of a ListBox Control. Many developers can do it many ways. Here I will explain in 3 ways. Those are follows:

1. Get Selected Items Using GetSelectedIndices
2. Get Selected Items Using For Each Loop
3. Get Selected Items Using For Loop

Related Articles:
1. Javascript to Get all Selected Values or Text from a Multiple Select Listbox of Asp.Net C# VB.Net
2. Jquery to Get all Selected Values or Text from a Multiple Select Listbox of Asp.Net C# VB.Net

Output/Outcome:
How to Get All Selected Items of ListBox in Asp.Net Csharp VB.Net

To do the Example Prepare the HTML Code Like below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Width="100" Height="200">
        <asp:ListItem Text="January" Value="1" />
        <asp:ListItem Text="Februray" Value="2" />
        <asp:ListItem Text="March" Value="3" />
        <asp:ListItem Text="April" Value="4" />
        <asp:ListItem Text="May" Value="5" />
        <asp:ListItem Text="June" Value="6" />
        <asp:ListItem Text="July" Value="7" />
        <asp:ListItem Text="August" Value="8" />
        <asp:ListItem Text="September" Value="9" />
        <asp:ListItem Text="October" Value="10" />
        <asp:ListItem Text="November" Value="11" />
        <asp:ListItem Text="December" Value="12" />
    </asp:ListBox>
    <hr />
    <asp:Button ID="Button1" runat="server" Text="Using GetSelectedIndices" onclick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="Using For Each Loop" 
        onclick="Button2_Click" />
    <asp:Button ID="Button3" runat="server" Text="Using For Loop" 
        onclick="Button3_Click" />
    <br /><br />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</asp:Content>

Get Selected Items Using GetSelectedIndices:
C# Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        // Using GetSelectedIndices
        Label1.Text = ""; 
        foreach (int i in ListBox1.GetSelectedIndices())
        {
            Label1.Text += "Selected Value: " + ListBox1.Items[i].Value + " Selected Text: " + ListBox1.Items[i].Text+"<br/>";
        }
    }

VB.Net Code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        ' Using GetSelectedIndices
        Label1.Text = ""
        For Each i As Integer In ListBox1.GetSelectedIndices()
            Label1.Text += ("Selected Value: " + ListBox1.Items(i).Value & " Selected Text: ") + ListBox1.Items(i).Text & "<br/>"
        Next
    End Sub

Get Selected Items Using For Each Loop:
C# Code:

    protected void Button2_Click(object sender, EventArgs e)
    {
        // Using Items collection
        Label1.Text = "";
        foreach (ListItem oItem in ListBox1.Items)
        {
            if (oItem.Selected)
            {
                Label1.Text += "Selected Value: " + oItem.Value + " Selected Text: " + oItem.Text + "<br/>";
            }
        }
    }

VB.Net Code:

    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        ' Using Items collection
        Label1.Text = ""
        For Each oItem As ListItem In ListBox1.Items
            If oItem.Selected Then
                Label1.Text += ("Selected Value: " + oItem.Value & " Selected Text: ") + oItem.Text & "<br/>"
            End If
        Next
    End Sub

Get Selected Items Using For Loop:
C# Code:

    protected void Button3_Click(object sender, EventArgs e)
    {
        // Using For Loop
        Label1.Text = "";
        for(int i=0;i<ListBox1.Items.Count;i++)
            if(ListBox1.Items[i].Selected)
                Label1.Text += "Selected Value: " + ListBox1.Items[i].Value + " Selected Text: " + ListBox1.Items[i].Text + "<br/>";
    }

VB.Net Code:

    Protected Sub Button3_Click(sender As Object, e As EventArgs)
        ' Using For Loop
        Label1.Text = ""
        For i As Integer = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).Selected Then
                Label1.Text += ("Selected Value: " + ListBox1.Items(i).Value & " Selected Text: ") + ListBox1.Items(i).Text & "<br/>"
            End If
        Next
    End Sub

Hope now you can Get or Read all Seleted Items of a ListBox from Server side or from code behind code.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, ListBox, VB.Net
2 comments on “How to Get All Selected Items of a ListBox in Asp.Net C# VB.Net
  1. Mike says:

    This code in the aspx file (HTML): …caused a “Master Page error, so I removed it.”

  2. Tushar Pawar says:

    I am having problem when i set Autopostback =”true” of listbox. My listbox not getting selected value after postback.It gets reset to default values. My code: $(document).ready(function () { showList(); }); function showList() { $(‘[id*=lstList]’).multiselect({ includeSelectAllOption: true }); $(‘[id*=lstList]’).multiselect(‘selectAll’, false); $(‘[id*=lstList]’).multiselect(‘updateButtonText’); }

Leave a Reply

Your email address will not be published. Required fields are marked *

     

*