asp.net listbox selected items count
ListBox is an asp.net list web server control. this server control allow us to select a single item or
multiple items at a time. listbox has no built in property or method to directly count listbox selected items.
to get the listbox selected items number we need to apply few techniques.
First we write an event handler for the listbox SelectedIndexChanged event. when listbox items selection changed we apply a linq query to get its selected items. finally in this linq query we count the selected items collection's objects by using Count() method. the Enumerable.Count() method allow us to get the number of elements in a sequence.
The following asp.net c# example code demonstrate us how can we count the listbox selected items programmatically in an asp.net application.
First we write an event handler for the listbox SelectedIndexChanged event. when listbox items selection changed we apply a linq query to get its selected items. finally in this linq query we count the selected items collection's objects by using Count() method. the Enumerable.Count() method allow us to get the number of elements in a sequence.
The following asp.net c# example code demonstrate us how can we count the listbox selected items programmatically in an asp.net application.
listbox-selected-items-count.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 count all selected items from listbox
var items = (from ListItem li in ListBox1.Items
where li.Selected == true
select li).Count();
Label1.Text = "you selected....<br /><b>";
Label1.Text += items + "</b> items";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net listbox selected items count</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - listbox selected items count
</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="Ochre-breasted Foliage-gleaner" Value="1"></asp:ListItem>
<asp:ListItem Text="Plain Xenops" Value="2"></asp:ListItem>
<asp:ListItem Text="White-eyed Foliage-gleaner" Value="3"></asp:ListItem>
<asp:ListItem Text="Plain-brown Woodcreeper" Value="4"></asp:ListItem>
<asp:ListItem Text="Scalloped Woodcreeper" Value="5"></asp:ListItem>
</asp:ListBox>
</div>
</form>
</body>
</html>

- How to make DropDownList first item not selectable
- How to set ListBox alternate item text and background color
- How to apply alternating row color in a ListBox
- How to show horizontal ScrollBar in a ListBox
- How to get ListBox multiple 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
- How to create a horizontal CheckBoxList
- How to show ScrollBar in a CheckBoxList