asp.net - How to count items of a CheckBoxList

Count CheckBoxList Items
The following asp.net c# example code demonstrate us how can we count CheckBoxList's total items programmatically at run time. CheckBoxList is an asp.net list web server control. CheckBoxList can be empty or can contain one or more items. Each item represent a ListItem object and render as an individual CheckBox control. CheckBoxList is a virtual group of multiple CheckBox controls.

CheckBoxList items exists in an items collection. So we can call any methods or properties from Colleaction<T> Class to manage CheckBoxList items collection. .Net framework Collection<T> Class 'Count' property allow us to get the number of total elements (items) exists in a collection. So we can count CheckBoxList control's items as CheckBoxListID.Items.Count. Count property return an integer value which represent the number of total items in a collection
CheckBoxListItemsCount.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        int totalItems;
        totalItems = CheckBoxList1.Items.Count;
        Label1.Text = "CheckBoxList Total Items: " + totalItems;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get number of total item from CheckBoxList, items count</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Maroon">CheckBoxList: Items Count</h2>
        <asp:Label 
             ID="Label1"
             runat="server"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2"
             runat="server"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             Text="asp.net controls"
             >
        </asp:Label>
        <asp:CheckBoxList 
             ID="CheckBoxList1"
             runat="server"
             BorderColor="SeaGreen"
             BorderWidth="2"
             >
             <asp:ListItem>EntityDataSource</asp:ListItem>
             <asp:ListItem>Calendar</asp:ListItem>
             <asp:ListItem>HiddenField</asp:ListItem>
             <asp:ListItem>ObjectDataSource</asp:ListItem>
             <asp:ListItem>RangeValidator</asp:ListItem>
        </asp:CheckBoxList>
        <br /><br />
        <asp:Button 
             ID="Button1"
             runat="server"
             Font-Bold="true"
             ForeColor="SeaGreen"
             Text="Count Items"
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples