CheckBox.OnCheckedChanged(EventArgs) Method

Definition

Raises the CheckedChanged event of the CheckBox control. This allows you to handle the event directly.

protected:
 virtual void OnCheckedChanged(EventArgs ^ e);
protected virtual void OnCheckedChanged (EventArgs e);
abstract member OnCheckedChanged : EventArgs -> unit
override this.OnCheckedChanged : EventArgs -> unit
Protected Overridable Sub OnCheckedChanged (e As EventArgs)

Parameters

e
EventArgs

A EventArgs that contains the event data.

Examples

The following example demonstrates how to specify and code the handler for the CheckedChanged event of a CheckBox control.

Note

The following code samples use the single-file code model and may not work correctly if copied directly into a code-behind file. Each 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 Forms Page Code Model.


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CheckBox CheckedChanged Example</title>
<script runat="server">
 
      void Check_Clicked(Object sender, EventArgs e) 
      {

         // Calculate the subtotal and display the result in currency format.
         // Include tax if the check box is selected.
         Message.Text = CalculateTotal(checkbox1.Checked).ToString("c");

      }

      void Page_Load(Object sender, EventArgs e)
      {

         // Display the subtotal without tax when the page is first loaded.
         if(!IsPostBack)
         {

            // Calculate the subtotal and display the result in currency format.
            Message.Text = CalculateTotal(false).ToString("c");

         }

      }

      double CalculateTotal(bool Taxable)
      {

         // Calculate the subtotal for the example.
         double Result = 1.99 + 2.99 + 3.99;

         // Add tax, if applicable.
         if(Taxable)
         {
            Result += Result * 0.086;
         }

         return Result; 

      }
 
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">
 
      <h3>CheckBox CheckedChanged Example</h3>

      Select whether to include tax in the subtotal.

      <br /><br />

      <table border="1" cellpadding="5">

         <tr>

            <th colspan="2">

               Shopping cart

            </th>

         </tr>

         <tr>

            <td>

               Item 1

            </td>

            <td>

               $1.99

            </td>

         </tr>

         <tr>

            <td>

               Item 2

            </td>

            <td>

               $2.99

            </td>

         </tr>

         <tr>

            <td>

               Item 3

            </td>

            <td>

               $3.99

            </td>

         </tr>

         <tr>

            <td>

               <b>Subtotal</b>

            </td>

            <td>

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

            </td>

         </tr>

         <tr>

            <td colspan="2">

               <asp:CheckBox id="checkbox1" runat="server"
                    AutoPostBack="True"
                    Text="Include 8.6% sales tax"
                    TextAlign="Right"
                    OnCheckedChanged="Check_Clicked"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>

<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CheckBox CheckedChanged Example</title>
<script runat="server">
 
      Sub Check_Clicked(sender As Object, e As EventArgs) 

         ' Calculate the subtotal and display the result in currency format.
         ' Include tax if the check box is selected.
         Message.Text = CalculateTotal(checkbox1.Checked).ToString("c")

      End Sub

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Display the subtotal without tax when the page is first loaded.
         If Not IsPostBack Then

            ' Calculate the subtotal and display the result in currency format.
            Message.Text = CalculateTotal(false).ToString("c")

         End If

      End Sub

      Function CalculateTotal(Taxable As Boolean) As Double 

         ' Calculate the subtotal for the example.
         Dim Result As Double = 1.99 + 2.99 + 3.99

         ' Add tax, if applicable.
         If(Taxable)

            Result += Result * 0.086
         
         End If

         Return Result 

      End Function
 
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">
 
      <h3>CheckBox CheckedChanged Example</h3>

      Select whether to include tax in the subtotal.

      <br /><br />

      <table border="1" cellpadding="5">

         <tr>

            <th colspan="2">

               Shopping cart

            </th>

         </tr>

         <tr>

            <td>

               Item 1

            </td>

            <td>

               $1.99

            </td>

         </tr>

         <tr>

            <td>

               Item 2

            </td>

            <td>

               $2.99

            </td>

         </tr>

         <tr>

            <td>

               Item 3

            </td>

            <td>

               $3.99

            </td>

         </tr>

         <tr>

            <td>

               <b>Subtotal</b>

            </td>

            <td>

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

            </td>

         </tr>

         <tr>

            <td colspan="2">

               <asp:CheckBox id="checkbox1" runat="server"
                    AutoPostBack="True"
                    Text="Include 8.6% sales tax"
                    TextAlign="Right"
                    OnCheckedChanged="Check_Clicked"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CheckBox CheckedChanged Example</title>
<script runat="server">
 
      void Check_Clicked(Object sender, EventArgs e) 
      {

         // Calculate the subtotal and display the result in currency format.
         // Include tax if the check box is selected.
         Message.Text = CalculateTotal(checkbox1.Checked).ToString("c");

      }

      void Page_Load(Object sender, EventArgs e)
      {

         // Display the subtotal without tax when the page is first loaded.
         if(!IsPostBack)
         {

            // Calculate the subtotal and display the result in 
            // currency format.
            Message.Text = CalculateTotal(false).ToString("c");

         }

         // Manually register the event-handling method for the   
         // CheckedChanged event of the CheckBox control.
         checkbox1.CheckedChanged += new EventHandler(this.Check_Clicked);

      }

      double CalculateTotal(bool Taxable)
      {

         // Calculate the subtotal for the example.
         double Result = 1.99 + 2.99 + 3.99;

         // Add tax, if applicable.
         if(Taxable)
         {
            Result += Result * 0.086;
         }

         return Result; 

      }
 
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">
 
      <h3>CheckBox CheckedChanged Example</h3>

      Select whether to include tax in the subtotal.

      <br /><br />

      <table border="1" cellpadding="5">

         <tr>

            <th colspan="2">

               Shopping cart

            </th>

         </tr>

         <tr>

            <td>

               Item 1

            </td>

            <td>

               $1.99

            </td>

         </tr>

         <tr>

            <td>

               Item 2

            </td>

            <td>

               $2.99

            </td>

         </tr>

         <tr>

            <td>

               Item 3

            </td>

            <td>

               $3.99

            </td>

         </tr>

         <tr>

            <td>

               <b>Subtotal</b>

            </td>

            <td>

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

            </td>

         </tr>

         <tr>

            <td colspan="2">

               <asp:CheckBox id="checkbox1" runat="server"
                    AutoPostBack="True"
                    Text="Include 8.6% sales tax"
                    TextAlign="Right"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>

<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CheckBox CheckedChanged Example</title>
<script runat="server">
 
      Sub Check_Clicked(sender As Object, e As EventArgs) 

         ' Calculate the subtotal and display the result in currency format.
         ' Include tax if the check box is selected.
         Message.Text = CalculateTotal(checkbox1.Checked).ToString("c")

      End Sub

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Display the subtotal without tax when the page is first loaded.
         If Not IsPostBack Then

            ' Calculate the subtotal and display the result in 
            ' currency format.
            Message.Text = CalculateTotal(false).ToString("c")

         End If

         ' Manually register the event-handling method for the   
         ' CheckedChanged event of the CheckBox control.
         AddHandler checkbox1.CheckedChanged, AddressOf Check_Clicked

      End Sub

      Function CalculateTotal(Taxable As Boolean) As Double 

         ' Calculate the subtotal for the example.
         Dim Result As Double = 1.99 + 2.99 + 3.99

         ' Add tax, if applicable.
         If(Taxable)

            Result += Result * 0.086
         
         End If

         Return Result 

      End Function
 
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">
 
      <h3>CheckBox CheckedChanged Example</h3>

      Select whether to include tax in the subtotal.

      <br /><br />

      <table border="1" cellpadding="5">

         <tr>

            <th colspan="2">

               Shopping cart

            </th>

         </tr>

         <tr>

            <td>

               Item 1

            </td>

            <td>

               $1.99

            </td>

         </tr>

         <tr>

            <td>

               Item 2

            </td>

            <td>

               $2.99

            </td>

         </tr>

         <tr>

            <td>

               Item 3

            </td>

            <td>

               $3.99

            </td>

         </tr>

         <tr>

            <td>

               <b>Subtotal</b>

            </td>

            <td>

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

            </td>

         </tr>

         <tr>

            <td colspan="2">

               <asp:CheckBox id="checkbox1" runat="server"
                    AutoPostBack="True"
                    Text="Include 8.6% sales tax"
                    TextAlign="Right"
                    OnCheckedChanged="Check_Clicked"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>

Remarks

The CheckedChanged event is raised when the value of the Checked property changes between posts to the server.

Note

A CheckBox control must persist some values between posts to the server for this event to work correctly. Be sure that view state is enabled for this control.

Raising an event invokes the event handler through a delegate. For more information, see How to: Consume Events in a Web Forms Application.

The OnCheckedChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors

When overriding OnCheckedChanged(EventArgs) in a derived class, be sure to call the base class's OnCheckedChanged(EventArgs) method so that registered delegates receive the event.

Applies to

See also