CheckBoxList Control in ASP.NET

Introduction & Demonstration

The CheckBoxList control renders a list of check boxes. The check boxes can be rendered horizontally or vertically. Unlike the other List controls, a user always can select multiple items when using a CheckBoxList control. For example, the page given below contains a CheckBoxList control that renders its list items in two columns.


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""">

<script runat="server">

    Protected Sub btnSubmit_Click(ByVal sender As ObjectByVal e As EventArgs)
        For Each item As ListItem In cblProducts.Items
            If (item.Selected) Then
                lblProduct.Text &= "<li>" & item.Text
            End If
    End Sub

html xmlns="" >
<head id="Head1" runat="server">
    <form id="form1" runat="server">


        Runat="server" />

        Runat="server" />

    <hr />

        Runat="server" />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE] FROM [PRO_LIST]"></asp:SqlDataSource>


When we click the Submit button, the values of the Text property of any selected check boxes are displayed in a Label control. The selected check boxes are retrieved from the CheckBoxList control's Items property.

The CheckBoxList control includes three properties that affect its layout:

  • RepeatColumns: The number of columns of check boxes to display.
  • RepeatDirection: The direction in which the check boxes are rendered. Possible values are Horizontal and Vertical.
  • RepeatLayout: Determines whether the check boxes are displayed in an HTML table. Possible values are Table and Flow.

Normally, a CheckBoxList control renders its list items in an HTML table. When the RepeatLayout property is set to the value Flow, the items are not rendered in a table.


Up Next
    Ebook Download
    View all
    View all