CheckBoxList Constructor ()


Initializes a new instance of the CheckBoxList class.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public CheckBoxList()

Use this constructor to create and initialize a new instance of the CheckBoxList class.

The following code example demonstrates how to create and initialize a new instance of the CheckBoxList control.


The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

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

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

<head runat="server">
    <title> CheckBoxList Constructor Example </title>
<script runat="server">

      void Check_Clicked(Object sender, EventArgs e) 

         // Retrieve the CheckBoxList control from the Controls collection
         // of the PlaceHolder control.
         CheckBoxList checklist = 

         // Make sure a control was found.
         if(checklist != null)

            Message.Text = "Selected Item(s):<br /><br />";

            // Iterate through the Items collection of the CheckBoxList 
            // control and display the selected items.
            for (int i=0; i<checklist.Items.Count; i++)

               if (checklist.Items[i].Selected)

                  Message.Text += checklist.Items[i].Text + "<br />";





            // Display an error message.
            Message.Text = "Unable to find CheckBoxList control.";



      void Page_Load(Object sender, EventArgs e)

         // Create a new CheckBoxList control.
         CheckBoxList checklist = new CheckBoxList();

         // Set the properties of the control.
         checklist.ID = "checkboxlist1";
         checklist.AutoPostBack = true;
         checklist.CellPadding = 5;
         checklist.CellSpacing = 5;
         checklist.RepeatColumns = 2;
         checklist.RepeatDirection = RepeatDirection.Vertical;
         checklist.RepeatLayout = RepeatLayout.Flow;
         checklist.TextAlign = TextAlign.Right;

         // Populate the CheckBoxList control.
         checklist.Items.Add(new ListItem("Item 1"));
         checklist.Items.Add(new ListItem("Item 2"));
         checklist.Items.Add(new ListItem("Item 3"));
         checklist.Items.Add(new ListItem("Item 4"));
         checklist.Items.Add(new ListItem("Item 5"));
         checklist.Items.Add(new ListItem("Item 6"));

         // Manually register the event-handling method for the 
         // SelectedIndexChanged event.
         checklist.SelectedIndexChanged += new EventHandler(this.Check_Clicked);

         // Add the control to the Controls collection of the 
         // PlaceHolder control.





   <form id="form1" runat="server">

      <h3> CheckBoxList Constructor Example </h3>

      Select items from the CheckBoxList.

      <br /><br />

      <asp:PlaceHolder id="Place" runat="server"/>

      <br /><br />

      <asp:label id="Message" runat="server"/>




.NET Framework
Available since 1.1
Return to top