Export (0) Print
Expand All

HtmlInputCheckBox.OnServerChange Method

Raises the ServerChange event. This method allows you to handle the event directly.

[Visual Basic]
Protected Overridable Sub OnServerChange( _
   ByVal e As EventArgs _
)
[C#]
protected virtual void OnServerChange(
 EventArgs e
);
[C++]
protected: virtual void OnServerChange(
 EventArgs* e
);
[JScript]
protected function OnServerChange(
   e : EventArgs
);

Parameters

e
A System.EventArgs that contains event information.

Remarks

This event is raised when the Web page is submitted to the server and the state of the HtmlInputCheckBox control changes state from the previous post. This method allows you to override the base implementation and provide a custom handler for the event.

Note   The control must have viewstate enabled for the ServerChange event to work correctly.

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnServerChange 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 OnServerChange in a derived class, be sure to call the base class's OnServerChange method so that registered delegates receive the event.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to specify and code a handler for the ServerChange event.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script language="VB" runat="server">

    Sub Button1_Click(sender As Object, e As EventArgs)
        
        If Prev_Check_State.InnerHtml = Check1.Checked.ToString() Then
            Span2.InnerHtml = "CheckBox1 did not change state between clicks."
        End If 
        If Check1.Checked = True Then
            Span1.InnerHtml = "CheckBox1 is checked!"
            Prev_Check_State.InnerHtml = "True"
        Else
            Span1.InnerHtml = "CheckBox1 is not checked!"
            Prev_Check_State.InnerHtml = "False"
        End If
    End Sub 'Button1_Click
     

    Sub Server_Changed(sender As Object, e As EventArgs)
        Span2.InnerHtml = "CheckBox1 changed state between clicks."
    End Sub 'Server_Changed

  </script>

</head>

<body>

   <h3>HtmlInputCheckBox Sample</h3>

   <form runat=server>

      <input id="Check1" 
             type="checkbox"
             OnServerChange="Server_Changed" 
             runat="server"/> 
 
      CheckBox1 &nbsp;&nbsp;

      <span id="Span1" 
            style="color:red" 
            runat="server"/>

      <p>

      <input type="button" 
             id="Button1" 
             value="Enter" 
             OnServerClick="Button1_Click" 
             runat="server"/>
      
      <br><br>

      <span id="Span2" 
            runat="server"/>

      <htmlinputhidden id="Prev_Check_State"
                       Visible="false"
                       runat="server"/>

   </form>

</body>
</html>
   

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

<html>
<head>

   <script language="C#" runat="server">

      void Button1_Click(object sender, EventArgs e) 
      { 

         if (Prev_Check_State.InnerHtml == Check1.Checked.ToString())
            Span2.InnerHtml = "CheckBox1 did not change state between clicks.";

         if (Check1.Checked == true)
         {          
            Span1.InnerHtml = "CheckBox1 is checked!";
            Prev_Check_State.InnerHtml="True";
         }         
         else
         { 
            Span1.InnerHtml = "CheckBox1 is not checked!";
            Prev_Check_State.InnerHtml="False";
         }
         
      }

      void Server_Changed(object sender, EventArgs e) 
      {
         Span2.InnerHtml = "CheckBox1 changed state between clicks.";
      }

   </script>

</head>

<body>

   <h3>HtmlInputCheckBox Sample</h3>

   <form runat=server>

      <input id="Check1" 
             type="checkbox"
             OnServerChange="Server_Changed" 
             runat="server"/> 
 
      CheckBox1 &nbsp;&nbsp;

      <span id="Span1" 
            style="color:red" 
            runat="server"/>

      <p>

      <input type="button" 
             id="Button1" 
             value="Enter" 
             OnServerClick="Button1_Click" 
             runat="server"/>
      
      <br><br>

      <span id="Span2" 
            runat="server"/>

      <htmlinputhidden id="Prev_Check_State"
                       Visible="false"
                       runat="server"/>

   </form>

</body>
</html>
   

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
<head>

   <script language="JSCRIPT" runat="server">

    function Button1_Click(sender : Object, e : EventArgs){
        
        if(Prev_Check_State.InnerHtml == Check1.Checked.ToString())
            Span2.InnerHtml = "CheckBox1 did not change state between clicks."
        if(Check1.Checked){
            Span1.InnerHtml = "CheckBox1 is checked!"
            Prev_Check_State.InnerHtml = "True"
        }else{
            Span1.InnerHtml = "CheckBox1 is not checked!"
            Prev_Check_State.InnerHtml = "False"
        }
    }

    function Server_Changed(sender : Object, e : EventArgs){
        Span2.InnerHtml = "CheckBox1 changed state between clicks."
    }

  </script>

</head>

<body>

   <h3>HtmlInputCheckBox Sample</h3>

   <form runat=server>

      <input id="Check1" 
             type="checkbox"
             OnServerChange="Server_Changed" 
             runat="server"/> 
 
      CheckBox1 &nbsp;&nbsp;

      <span id="Span1" 
            style="color:red" 
            runat="server"/>

      <p>

      <input type="button" 
             id="Button1" 
             value="Enter" 
             OnServerClick="Button1_Click" 
             runat="server"/>
      
      <br><br>

      <span id="Span2" 
            runat="server"/>

      <htmlinputhidden id="Prev_Check_State"
                       Visible="false"
                       runat="server"/>

   </form>

</body>
</html>
   

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

HtmlInputCheckBox Class | HtmlInputCheckBox Members | System.Web.UI.HtmlControls Namespace | ServerChange | System.EventArgs

Show:
© 2014 Microsoft