HtmlTextArea.OnServerChange Method (EventArgs)

 

Raises the ServerChange event of the HtmlTextArea control. This allows you to provide a custom handler for the event.

Namespace:   System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

Protected Overridable Sub OnServerChange (
	e As EventArgs
)

Parameters

e
Type: System.EventArgs

A System.EventArgs that contains the event data.

The ServerChange event is raised when the content of the HtmlTextArea control changes between posts to the server.

System_CAPS_noteNote

This event is only raised when the user initiates a post to the server, such as by clicking a submit button. This event does not cause a post to the server to occur.

System_CAPS_noteNote

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

This event is commonly used to perform data validation on the HtmlTextArea control when the user updates the text in the control.

Raising an event invokes the event handler through a delegate. For more information, see NIB: 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.

The following code example demonstrates how to specify and create a custom event handler for the ServerChange event. A message is displayed when the value entered in the HtmlTextArea control exceeds 20 characters.

<%@ 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">
<script runat="server">

  Sub Server_Change(ByVal sender As Object, ByVal e As EventArgs)

    ' The ServerChange event is commonly used for data validation.
    ' This method determines whether the comment entered into the
    ' HtmlTextArea control is longer than 20 characters.
    If TextArea1.Value.Length > 20 Then
      Span1.InnerHtml = "Your comment cannot exceed 20 characters."
    Else
      Span1.InnerHtml = "You wrote: <br />" + TextArea1.Value
    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>HtmlTextArea ServerChange Example</title>

</head>
<body>

   <form id="form1" runat="server">

      <h3>HtmlTextArea ServerChange Example</h3>

      Enter your comments: <br />

      <textarea rows="2" cols="20" id="TextArea1"
                onserverchange="Server_Change" 
                runat="server"/>

      <br />

      <input type="submit"  
             value="Submit" 
             runat="server"/>

      <br />

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

   </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">
<script runat="server">

  Sub Server_Change(ByVal sender As Object, ByVal e As EventArgs)

    ' The ServerChange event is commonly used for data validation.
    ' This method determines whether the comment entered into the
    ' HtmlTextArea control is longer than 20 characters.
    If TextArea1.Value.Length > 20 Then

      Span1.InnerHtml = "Your comment cannot exceed 20 characters."

    Else

      Span1.InnerHtml = "You wrote: <br />" + TextArea1.Value

    End If

  End Sub

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Create an EventHandler delegate for the method you want to 
    ' handle the event, and then add it to the list of methods
    ' called when the event is raised.
    AddHandler TextArea1.ServerChange, AddressOf Server_Change

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
   <title>HtmlTextArea ServerChange Example</title>
</head>

<body>

   <form id="form1" runat="server">

      <h3>HtmlTextArea ServerChange Example</h3>

      Enter your comments (20 or fewer characters): <br />

      <textarea rows="2" cols="20" id="TextArea1"
                runat="server"/>

      <br />

      <input type="submit"  
             value="Submit" 
             runat="server"/>

      <br />

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

   </form>

</body>
</html>

.NET Framework
Available since 1.1
Return to top
Show: