Friday, March 29, 2013

Get All selected values from listbox in asp.net OR how to get listbox value codebehind multiple value in asp.net

Here i will show how to get all selected values from ListBox in asp.net

Out Put :






ASPX.Net :




<asp:ListBox ID="ListBox1" runat="server" Height="130px" 
        OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" AutoPostBack="True" 
        SelectionMode="Multiple">
        <asp:ListItem Value="1" Text="One"></asp:ListItem>
        <asp:ListItem Value="2" Text="Two"></asp:ListItem>
        <asp:ListItem Value="3" Text="Three"></asp:ListItem>
        <asp:ListItem Value="4" Text="Four"></asp:ListItem>
        <asp:ListItem Value="5" Text="Five"></asp:ListItem>
        <asp:ListItem Value="6" Text="Six"></asp:ListItem>
        <asp:ListItem Value="7" Text="Seven"></asp:ListItem>
        <asp:ListItem Value="8" Text="Eight"></asp:ListItem>
        <asp:ListItem Value="9" Text="Nine"></asp:ListItem>
    </asp:ListBox>
    <br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>


C# :  

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = "Selected Values : " 
String.Join(", ", ListBox1.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToArray()); ;
}


No comments:

Post a Comment