asp.net checkboxlist checked items
CheckBoxList is an asp.net list web server control. checkboxlist render a multi selection check box group on a web page
which allow users to select multiple items at a time. we can get the checkboxlist selected (checked) items programmatically
by using its SelectedIndexChanged event. we also can display users selected items list on web page.
To do this, first we need to write an event handler for checkboxlist SelectedIndexChanged event. when users select or deselect an item from checkboxlist then the SelectedIndexChanged event fire. after the event occurs, we apply a linq query to get a list of only selected items. next we loop through the selected items collection and display the items in a label control.
The following asp.net c# example code demonstrate us how can we display checkboxlist checked items in a web page in an asp.net application.
To do this, first we need to write an event handler for checkboxlist SelectedIndexChanged event. when users select or deselect an item from checkboxlist then the SelectedIndexChanged event fire. after the event occurs, we apply a linq query to get a list of only selected items. next we loop through the selected items collection and display the items in a label control.
The following asp.net c# example code demonstrate us how can we display checkboxlist checked items in a web page in an asp.net application.
checkboxlist-checked-items.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = {
"Ringed Plover",
"Kentish Plover",
"Pheasant-tailed Jacana",
"American Woodcock",
"Hudsonian Godwit",
"Bar-tailed Godwit"
};
CheckBoxList1.DataSource = birds;
CheckBoxList1.DataBind();
}
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
var items = from ListItem li in CheckBoxList1.Items
where li.Selected == true
select li;
Label1.Text = "you checked item(s).....<br />";
foreach (ListItem li in items)
{
Label1.Text += li.Text + " | " + li.Value + "<br />";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net checkboxlist checked items</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - checkboxlist checked items
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Text="check item(s) from checkboxlist."
Font-Size="Medium"
Width="350"
>
</asp:Label>
<br /><br />
<div class="block">
<asp:CheckBoxList
ID="CheckBoxList1"
runat="server"
RepeatLayout="Table"
RepeatColumns="2"
AutoPostBack="true"
OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"
>
</asp:CheckBoxList>
</div>
</div>
</form>
</body>
</html>


- How to sort DropDownList items
- How to sort ListBox items
- How to sort ListBox items alphabetically
- How to set default selected items in a ListBox
- How to disable specific items in a ListBox
- How to select all items in a CheckBoxList
- How to retrieve multiple selected values from CheckBoxList
- How to set default checked items in CheckBoxList
- How to get CheckBoxList selected values using Linq
- How to set the maximum limit of selection in a CheckBoxList