Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 RegisterForEventValidation Method (...
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ClientScriptManager..::.RegisterForEventValidation Method (String)

Registers an event reference for validation with a unique control ID representing the client control generating the event.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public Sub RegisterForEventValidation ( _
    uniqueId As String _
)
Visual Basic (Usage)
Dim instance As ClientScriptManager
Dim uniqueId As String

instance.RegisterForEventValidation(uniqueId)
C#
public void RegisterForEventValidation(
    string uniqueId
)
Visual C++
public:
void RegisterForEventValidation(
    String^ uniqueId
)
JScript
public function RegisterForEventValidation(
    uniqueId : String
)

Parameters

uniqueId
Type: System..::.String
A unique ID representing the client control generating the event.

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.

Visual Basic
<%@ Page Language="VB" %>
<%@ 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">

    Dim _cbMessage As String = ""
    ' Define method that processes the callbacks on server.
    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
    Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent

        Try
            Page.ClientScript.ValidateEvent(button1.UniqueID)
            _cbMessage = "Correct event raised callback."

        Catch ex As Exception
            _cbMessage = "Incorrect event raised callback."

        End Try

    End Sub

    ' Define method that returns callback result.
    Public Function GetCallbackResult() _
    As String Implements _
    System.Web.UI.ICallbackEventHandler.GetCallbackResult

        Return _cbMessage

    End Function


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        If (Not IsPostBack) Then
            Dim cs As ClientScriptManager = Page.ClientScript
            Dim cbReference As String = cs.GetCallbackEventReference("'" & _
                Page.UniqueID & "'", "arg", "ReceiveServerData", "", _
                "ProcessCallBackError", False)
            Dim callbackScript As String = "function CallTheServer(arg, context) {" & _
                cbReference & "; }"
            cs.RegisterClientScriptBlock(Me.GetType(), "CallTheServer", _
                callbackScript, True)

        End If
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

        Page.ClientScript.RegisterForEventValidation(button1.UniqueID)
        MyBase.Render(writer)
    End Sub

</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  >
<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>
C#
<%@ 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);
            _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);
        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  >
<head id="Head1" 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.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
This help content is so not useful      yoshiw   |   Edit   |   Show History
a remark section with some concept explanation would help
Tags What's this?: Add a tag
Flag as ContentBug
Please give a fully functional example      triashill   |   Edit   |   Show History

thanx for the illustration but can u give a fully functional example which i can either test or at least follow

Tags What's this?: amen (x) Add a tag
Flag as ContentBug
Here's a Sample usage      jalchr ... stupid2533   |   Edit   |   Show History

Usage: When you want to manipulate some server controls using client side javascript.

Steps:

Override the page ... register all controls which you want to bypass the event validation for.

protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(DropDownList1.UniqueID);
Page.ClientScript.RegisterForEventValidation(DropDownList2.UniqueID);

Page.ClientScript.RegisterForEventValidation(ListBox1.UniqueID);

// .... etc
base.Render(writer);
}
Tags What's this?: Add a tag
Flag as ContentBug
client side javascript?      Geert-Jan Thomas   |   Edit   |   Show History
Is there a sample of javascript that would complement this server side code?

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker