Share via


CheckBox control

You can use two types of ASP.NET controls to add check boxes to a Web Forms page: individual CheckBox controls or a CheckBoxList control. Both controls provide a way for users to input Boolean data: true or false, yes or no.

CheckBox control versus CheckBoxList control

You add individual CheckBox controls to a page and work with them singly. Alternatively, the CheckBoxList control is a single control that acts as a parent control for a collection of check-box list items. It derives from a base ListControl class, and therefore works much like the ListBox control, DropDownList control, RadioButtonList control, and BulletedList control controls. For that reason, many of the procedures for working with the CheckBoxList control are the same as the procedures for the other list web server controls.

Each type of control has advantages. By using individual CheckBox controls, you get more control over the layout of the check boxes on the page than by using the CheckBoxList control. For example, you can include text (that is, non-check-box text) between each check box. You can also control the font and color of individual check boxes.

The CheckBoxList control is a better choice if you want to create a series of check boxes from data in a database. (You can still bind an individual CheckBox control to data.)

For more information about managing CheckBox and CheckBoxList controls, see CheckBox and CheckBoxList Web Server Controls (Visual Studio) in the MSDN library.

To add a CheckBox control to a page

  1. Drag the CheckBox control from the Toolbox task pane to your page.

  2. Specify a caption by setting the Text property.

  3. Optionally, change the orientation of the caption by setting the TextAlign property.

When the CheckBox control renders to the browser, it does so in two parts: an input element representing the check box, and a separate label element representing the caption for the check box. The combination of the two elements is in turn wrapped in a span element.

When you apply style or attribute settings to a CheckBox control, the settings are applied to the outer span element. For example, if you set the control's BackColor property, the setting is applied to the span element, and therefore it affects both the inner input and label attributes.

See also

Concepts

ASP.NET

ASP.NET standard controls

ASP.NET data controls

ASP.NET validation controls

ASP.NET navigation controls

ASP.NET login controls

ASP.NET web parts controls