How to Count Selected Item of CheckBoxList in ASP.Net

Hi all, here in this asp.net tutorials we will learn to count how many item selected on checkboxlist control in ASP.Net.

- We all are aware of CheckBoxList control in ASP.Net. So, we all know how to select item on CheckBoxList Control, Here we just learn to count that Select Item of CheckBoxList control in ASP.Net with an Example.

STEP 1 – First Design the ASP.Net webpage with one CheckBoxList Control, one Button Control and one Label Control for dispaly the result of Example.

- The HTML Design code for ASP.Net Example is:

<table style=”width: 464px”>
<tr>
<td colspan=”1″ style=”width: 257px; height: 24px”>
<strong><span style=”font-size: 14pt; color: #6600cc; font-family: Arial”>Count Selected
Item – CheckBoxList</span></strong></td>
</tr>
<tr>
<td style=”width: 257px; height: 24px; text-align: center;”>
&nbsp;
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Count Selected” Font-Bold=”True” Font-Size=”Medium” />&nbsp;</td>
</tr>
<tr>
<td style=”width: 257px; height: 22px; text-align: center;”>
<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” Width=”168px”>
</asp:CheckBoxList>
<asp:Label ID=”Label1″ runat=”server”></asp:Label></td>
</tr>
<tr>
<td style=”width: 257px; height: 22px; text-align: center”>
</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
</table>

 

- The ASP.Net Example of CheckBoxList Selected Item Count design output is:

Count Selected items of CheckBoxList Control in ASP.Net with C#

Count Selected items of CheckBoxList Control in ASP.Net with C#

STEP 2 – Write the below code at code behing page of ASP.Net webform.

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{

string[] Days = { “Sunday”, “Monday”, “Tusday”, “Wednesday”, “Thursday” };
CheckBoxList1.DataSource = Days;
CheckBoxList1.DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
int count=0;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected == true)
{
count=count+ 1;
}
}
Label1.Text =”Selectd Item = “+ count.ToString();
}

 

- The ASP.Net Example of CheckBoxList Selected Item Count output is:

Count Selected items of CheckBoxList Control in ASP.Net with C#

Count Selected items of CheckBoxList Control in ASP.Net with C#

- The above asp.net result display the Three items selected on checkboxlist control in asp.net.

- This ASP.Net Example of CheckBoxList Control Selected Item Count will help you……

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 4 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">