Controls You Can Use on Web Forms ASP.NET Standard Controls CheckBox and CheckBoxList Controls
When the user selects a CheckBox control, the control raises an event that you can respond to.
NOTE: The CheckBoxList control raises events differently than individual CheckBox controls. For details, see Responding to Changes in a List Control.
You might not need to respond directly to the selection event of a CheckBox control at all. You respond to the event only if it’s important to know when the user has changed the check box selection.
If you are only interested in the state of the check box when selected, and not whether it has changed, you can simply test the check box after the form has been posted to the server by a control such as a Button control. For details about determining whether a CheckBox control is selected, see Getting and Setting a CheckBox Control Value Programmatically.
- Define a method to handle the control’s CheckedChanged event ( occurs when a user clicks on the control ). For example
void myCheckChangedHandler ( object src, EventArgs e ) {
// ... do your thing here ...
}
Sub myCheckChangedHandler ( ByVal src As Object, ByVal e As EventArgs )
' ... do your thing here ...
End Sub |
|
C# |
VB |
- Assign the handler to the onCheckedChanged event attribute of the CheckBox.
<asp:CheckBox onCheckedChanged="myCheckChangedHandler" runat="server" />
By default, the CheckedChanged event does not immediately cause the Web Forms page to be posted to the server. Instead, the event is raised in server code the next time the form is posted, as when a Button Web server control is clicked. To have the CheckedChanged event cause an immediate posting, set the CheckBox control’s AutoPostBack property to true.
Adding CheckBox Controls to a Web Forms Page Getting and Setting a CheckBox Control Value Programmatically