ClientScriptManager.RegisterForEventValidation Method (String, String)
Registers an event reference for validation with a unique control ID and event arguments representing the client control generating the event.
Assembly: System.Web (in System.Web.dll)
Parameters
- uniqueId
- Type: System.String
A unique ID representing the client control generating the event.
- argument
- Type: System.String
Event arguments passed with the client event.
| Exception | Condition |
|---|---|
| InvalidOperationException | The method is called prior to the Render method. |
The following code example demonstrates using the RegisterForEventValidation method and the ValidateEvent method to register a callback for validation and to validate that the callback originated from the page. To improve on the validation shown in the example, you could modify the validation argument parameter to contain information specific to the user such as an identity or role
<%@ Page Language="C#" %> <%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server" > string _cbMessage = ""; // Define method that processes the callbacks on server. public void RaiseCallbackEvent(String eventArgument) { try { Page.ClientScript.ValidateEvent(button1.UniqueID, this.ToString()); _cbMessage = "Correct event raised callback."; } catch (Exception ex) { _cbMessage = "Incorrect event raised callback."; } } // Define method that returns callback result. public string GetCallbackResult() { return _cbMessage; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ClientScriptManager cs = Page.ClientScript; String cbReference = cs.GetCallbackEventReference("'" + Page.UniqueID + "'", "arg", "ReceiveServerData", "", "ProcessCallBackError", false); String callbackScript = "function CallTheServer(arg, context) {" + cbReference + "; }"; cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer", callbackScript, true); } } protected override void Render(HtmlTextWriter writer) { Page.ClientScript.RegisterForEventValidation(button1.UniqueID, this.ToString()); base.Render(writer); } </script> <script type="text/javascript"> var value1 = new Date(); function ReceiveServerData(arg, context) { Message.innerText = arg; Label1.innerText = "Callback completed at " + value1; value1 = new Date(); } function ProcessCallBackError(arg, context) { Message.innerText = 'An error has occurred.'; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>CallBack Event Validation Example</title> </head> <body> <form id="Form1" runat="server"> <div> Callback result: <span id="Message"></span> <br /> <br /> <input type="button" id="button1" runat="server" value="ClientCallBack" onclick="CallTheServer(value1, null )"/> <br /> <br /> <asp:Label id="Label1" runat="server"/> </div> </form> </body> </html>
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.