asp.net listbox get multiple selected items
The following asp.net c# example code demonstrate us how can we get ListBox control's selected items
programmatically when ListBox selection mode is multiple.
ListBox control 'SelectionMode' property have two possible values 'Multiple' and 'Single'. Multiple mode allow us to select more than one items from ListBox control. When users select items from ListBox which SelectionMode is Multiple, then it is more complicated to get the selected items programmatically at run time.
We can get ListBox selected items on ListBox SelectedIndexChanged event or manually by a Button click without AutoPostBack. To get the ListBox selected items, at first we perform a Linq query to get the ListBox items which ListItem object's 'Selected' property value is 'true'. Then we loop through the selected items collection and display on web browser with items text and value..
ListBox control 'SelectionMode' property have two possible values 'Multiple' and 'Single'. Multiple mode allow us to select more than one items from ListBox control. When users select items from ListBox which SelectionMode is Multiple, then it is more complicated to get the selected items programmatically at run time.
We can get ListBox selected items on ListBox SelectedIndexChanged event or manually by a Button click without AutoPostBack. To get the ListBox selected items, at first we perform a Linq query to get the ListBox items which ListItem object's 'Selected' property value is 'true'. Then we loop through the selected items collection and display on web browser with items text and value..
listbox-get-multiple-selected-items.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//auto height of listbox to remove vertical scrollbar
ListBox1.Rows = ListBox1.Items.Count;
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//linq query to get all selected items from listbox
var items = from ListItem li in ListBox1.Items
where li.Selected == true
select li;
Label1.Text = "you selected....<br />";
//loop through selected items in listbox
foreach(ListItem li in items)
{
//selected item text and value.
Label1.Text +=li.Text + " | "+li.Value + "<br />";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net listbox get multiple selected items</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - listbox get multiple selected items
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="select item(s) from listbox."
Font-Size="X-Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:ListBox
ID="ListBox1"
runat="server"
AutoPostBack="true"
Font-Size="X-Large"
SelectionMode="Multiple"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
Font-Names="Comic Sans MS"
>
<asp:ListItem Text="Blackish Cinclodes" Value="1"></asp:ListItem>
<asp:ListItem Text="Canyon Canastero" Value="2"></asp:ListItem>
<asp:ListItem Text="Rufous Hornero" Value="3"></asp:ListItem>
<asp:ListItem Text="Thorn-tailed Rayadito" Value="4"></asp:ListItem>
<asp:ListItem Text="Firewood-gatherer" Value="5"></asp:ListItem>
</asp:ListBox>
</div>
</form>
</body>
</html>

- How to databind Label
- How to add attributes to DropDownList items
- How to sort DropDownList items
- How to sort ListBox items
- How to set ListBox alternate item text and background color
- How to show horizontal ScrollBar in a ListBox
- How to count ListBox selected items
- How to remove selected items from ListBox
- How to check whether an item exists in a ListBox
- How to add attributes to a ListBox items