HtmlTextArea.OnServerChange Method
Raises the ServerChange event of the HtmlTextArea control. This allows you to provide a custom handler for the event.
[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 the event data.
Remarks
The ServerChange event is raised when the content of the HtmlTextArea control changes between posts to the server.
Note 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.
Note 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 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 create a custom event handler for the ServerChange event. A message is displayed when the value entered in the HtmlTextArea control exceeds 10 characters.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub Server_Change(sender As Object, e As EventArgs) If TextArea1.Value.Length > 10 Then Span1.InnerHtml = "Your comment cannot exceed 10 characters." Else Span1.InnerHtml = "You wrote: <br>" + TextArea1.Value End If End Sub </script> </head> <body> <form runat=server> <h3>HtmlTextArea Example</h3> Enter your comments: <br> <textarea id="TextArea1" OnServerChange="Server_Change" runat="server"/> <br> <input type="submit" value="Submit" runat="server"/> <p> <span id="Span1" runat="server" /> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> void Server_Change(Object sender, EventArgs e) { if (TextArea1.Value.Length > 10) Span1.InnerHtml = "Your comment cannot exceed 10 characters."; else Span1.InnerHtml = "You wrote: <br>" + TextArea1.Value; } </script> </head> <body> <form runat=server> <h3>HtmlTextArea Example</h3> Enter your comments: <br> <textarea id="TextArea1" OnServerChange="Server_Change" runat="server"/> <br> <input type="submit" value="Submit" runat="server"/> <p> <span id="Span1" runat="server" /> </form> </body> </html> [JScript] <%@ Page Language="JScript" AutoEventWireup="True" %> <html> <head> <script runat="server"> function Server_Change(sender, e : EventArgs) { if (TextArea1.Value.Length > 10) Span1.InnerHtml = "Your comment cannot exceed 10 characters."; else Span1.InnerHtml = "You wrote: <br>" + TextArea1.Value; } </script> </head> <body> <form runat=server> <h3>HtmlTextArea Example</h3> Enter your comments: <br> <textarea id="TextArea1" OnServerChange="Server_Change" runat="server"/> <br> <input type="submit" value="Submit" runat="server"/> <p> <span id="Span1" runat="server" /> </form> </body> </html> [Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub Server_Change(sender As Object, e As EventArgs) ' The ServerChange event is commonly used for data validation. ' This method determines whether the comment entered into the ' 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(sender As Object, 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> </head> <body> <form runat=server> <h3>HtmlTextArea ServerChange Example</h3> Enter your comments (20 or fewer characters): <br> <textarea id="TextArea1" runat="server"/> <br> <input type="submit" value="Submit" runat="server"/> <p> <span id="Span1" runat="server" /> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> void Server_Change(Object sender, EventArgs e) { // The ServerChange event is commonly used for data validation. // This method determines whether the comment entered into the // the HtmlTextArea control is longer than 20 characters. if (TextArea1.Value.Length > 20) { Span1.InnerHtml = "Your comment cannot exceed 20 characters."; } else { Span1.InnerHtml = "You wrote: <br>" + TextArea1.Value; } } void Page_Load(Object sender, EventArgs e) { // 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. TextArea1.ServerChange += new System.EventHandler(this.Server_Change); } </script> </head> <body> <form runat=server> <h3>HtmlTextArea ServerChange Example</h3> Enter your comments (20 or fewer characters): <br> <textarea id="TextArea1" runat="server"/> <br> <input type="submit" value="Submit" runat="server"/> <p> <span id="Span1" 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
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
HtmlTextArea Class | HtmlTextArea Members | System.Web.UI.HtmlControls Namespace | ServerChange | System.EventArgs