We can use two types of ASP.net controls to add a check box to a Web forms page: A separate CheckBox control or a CheckBoxList control. Both controls provide a way for the user to enter Boolean data (TRUE or FALSE, yes or no).
Here we use the checkbox alone and look at its properties first
Property |
Description |
. NET |
AutoPostBack |
Specify whether the form will be returned to the server immediately after the Checked property has changed. The default is False. |
1.0 |
CausesValidation |
Specify whether to perform validation when clicking the Button control. |
2.0 |
Checked |
Specify whether the check box is selected. |
1.0 |
Inputattributes |
The collection of property names and values used by the Input element of the CheckBox control. |
2.0 |
Labelattributes |
A collection of property names and values used by the Label element of the CheckBox control. |
2.0 |
Runat |
Specify that the control is a server control. Must be set to "server". |
1.0 |
Text |
The text label associated with the check box. |
1.0 |
TextAlign |
The alignment of the text label associated with the check box. (right or left) |
1.0 |
ValidationGroup |
The group of controls to validate when a CheckBox control is sent back to the server. |
2.0 |
OnCheckedChanged |
The name of the function to be executed when the Checked property is changed. |
|
Let's do a simple example to illustrate
Foreground code:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "CheckBox.aspx.cs" inherits= "Webcontrols_checkbox"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Background code:
Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
public partial class WebControls_CheckBox:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void btnSubmit_Click (object sender, EventArgs e)
{
Lblstate.text = String. Empty;
if (chksport.checked)
{
Lblstate.text = Lblstate.text + chksport.text;
}
if (chksport2.checked)
{
if (lblState.Text.Length = = 0)
{
lblstate.text = Chksport2.text;
}
Else
{
Lblstate.text = Lblstate.text + "," + Chksport2.text;
}}
if (chksport3.checked)
{
if (lblState.Text.Length = = 0)
{
lblstate.text = Chksport2.text;
}
Else
{
Lblstate.text = Lblstate.text + ', ' + Chksport3.text;
}
}
}
}
Operation Effect: