This documentation is archived and is not being maintained.

CheckBoxList Constructor

Initializes a new instance of the CheckBoxList class.

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

public CheckBoxList ()
public CheckBoxList ()
public function 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 Page Code Model.

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



   <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 runat="server">
      <h3> CheckBoxList Constructor Example </h3>

      Select items from the CheckBoxList.


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

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


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0