In this tutorial, I will show you how to iterate through the selected items of CheckBoxList Control and obtaining the values. The checkboxlist control has more than one check box and more than one check box can be selected. All these selected values are stored in the checkboxlist item’s collection. We can check whether the check box is checked and if it is checked we will obtain the values.

For this we will use the for statement and if statement in C# and a label control to display the result of selected lists and total number of lists. The code for both the aspx and C# is show below.

CheckBoxListControl.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBoxListControl.aspx.cs"
 Inherits="CheckBoxListControl" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
 <title></title>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 <asp:Label ID="lbl_Result" runat="server" Text=""></asp:Label>
 <br />
 <asp:CheckBoxList ID="CheckBoxList1" runat="server">
 <asp:ListItem Value="0">Sunday</asp:ListItem>
 <asp:ListItem Value="1">Monday</asp:ListItem>
 <asp:ListItem Value="2">Tuesday</asp:ListItem>
 <asp:ListItem Value="3">Wednesday</asp:ListItem>
 <asp:ListItem Value="4">Thursday</asp:ListItem>
 <asp:ListItem Value="5">Friday</asp:ListItem>
 <asp:ListItem Value="6">Saturday</asp:ListItem>
 </asp:CheckBoxList>
 <br />
 <asp:Button ID="Button1" runat="server" Text="Find Selection" OnClick="Button1_Click" />
 </div>
 </form>
 </body>
 </html>
 

CodeBehind CheckBoxListControl.cs

using System;

 public partial class CheckBoxListControl : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {

 }
 protected void Button1_Click(object sender, EventArgs e)
 {
 int j = 0;

 lbl_Result.Text = "";

 for (int i = 0; i < CheckBoxList1.Items.Count; i++)
 {
 if (CheckBoxList1.Items[i].Selected)
 {

 lbl_Result.Text += "<br/>Selected Text : " + CheckBoxList1.Items[i].Text;

 j++;

 }

 }
 lbl_Result.Text += "<br/>No of selected items : " + j.ToString();

 }
 }
 

If you want to find the selected values. you can modify the code to

lbl_Result.Text += “Selected Text : ” + CheckBoxList1.Items[i].Value;