Compartilhar via


How to: Implement Callbacks in ASP.NET Web Pages

In a client callback, a client script function sends a request to the ASP.NET Web page, which then runs an abbreviated version of its normal life cycle to process the callback. To ensure that callback events originate from the expected user interface (UI), you can validate callbacks. In callback validation, you register an event for validation during the Web page rendering and then validate the event during the callback. For an overview of callbacks, see Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages.

To implement the ICallBackEventHandler interface

  1. For a single-file page or user control, implement the ICallbackEventHandler interface using an @ Implements directive in the page, as shown in the following example.

    <%@ Page Language="VB" %>
    <%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
    
    <%@ Page Language="C#" %>
    <%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
    

    Note

    If you are using a code-behind page model, implement the ICallbackEventHandler interface for your partial class.

  2. Implement the RaiseCallbackEvent method of the ICallbackEventHandler interface. The RaiseCallbackEvent method takes a single argument that represents the event arguments, as shown in the following example.

    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
    Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
    
    End Sub
    
    public void RaiseCallbackEvent(String eventArgument)
    {
    
    }
    
  3. Implement the GetCallbackResult method of the ICallbackEventHandler interface. The GetCallbackResult method takes no arguments and returns a string that represents the result of the callback. In the following example, a string called returnValue is returned.

    Public Function GetCallbackResult() _
    As String Implements _
    System.Web.UI.ICallbackEventHandler.GetCallbackResult
    
        Return returnValue
    
    End Function
    
    public String GetCallbackResult()
    {
        return returnValue;
    }
    

To register the callback for event validation

  • Override the Render method of the Page class and use the RegisterForEventValidation method of the ClientScriptManager class to register an event for validation. You can get a reference to the ClientScriptManager class by using the ClientScript property of the Page class. In the following example, a callback named Callback1 is registered for event validation.

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    
        Page.ClientScript.RegisterForEventValidation("ClientCallback1")
        MyBase.Render(writer)
    
    End Sub
    
    protected override void Render(HtmlTextWriter writer)
    {
        Page.ClientScript.RegisterForEventValidation("ClientCallback1");
        base.Render(writer);
    }
    

To validate the callback and return the callback result

  • In the RaiseCallbackEvent method, use the ValidateEvent method of the ClientScriptManager class to validate the event. Use the same method signature as the one used when registering the event for validation. In the following example, the method signature that takes one argument is used.

    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _
    Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        Try
            Page.ClientScript.ValidateEvent("ClientCallback1")
            ' Callback logic goes here.
            returnValue = "callback result"
    
        Catch
            ' Failed callback validation logic.
        End Try
    
    End Sub
    
    public void RaiseCallbackEvent(String eventArgument)
    {
        try
        {
            Page.ClientScript.ValidateEvent("ClientCallback1");
            // Callback logic goes here.
            returnValue = "callback result";
        }
        catch
        {
            // Failed callback validation logic.
        } 
    }
    

    If validation passes, your code should proceed with the callback event logic. After the RaiseCallbackEvent method has completed, the GetCallbackResult method is invoked to return the callback result as a string to a client script function.

    For more information about creating and implementing the client script functions to support client callbacks, see Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages and Client Callback with Validation Implementation Example.

See Also

Concepts

ASP.NET Client Script

Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages

Client Callback with Validation Implementation Example