This documentation is archived and is not being maintained.

Responding to User Selection in a CheckBox Web Server Control

Visual Studio .NET 2003

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 Web Server Control.

You might not need to respond directly to the check 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 Web Server Control Value Programmatically.

To respond to a selection in a CheckBox control

  • Create an event handler for the control's CheckedChanged event.

    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.

    Note   The ability of a CheckBox control to post to the server when it is checked requires that the browser support ECMAScript (JScript, JavaScript) and that scripting be enabled on the user's browser.

    The following example displays "True" or "False" in a Label Web server control to reflect the latest change in a CheckBox control.

    ' Visual Basic
    Private Sub CheckBox1_CheckedChanged(ByVal sender As _
       System.Object, ByVal e As System.EventArgs) Handles _
       CheckBox1.CheckedChanged
       Label1.Text = "CheckBox1 selection is: " & CheckBox1.Checked
    End Sub
    
    // C#
    private void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
       Label1.Text = "CheckBox1 selection is: " + CheckBox1.Checked;
    }
    

See Also

ASP.NET Server Control Event Model | CheckBox Class | CheckBoxList Class | Specifying Whether Controls Post to the Server

Show: