Wednesday, April 7, 2010

Implementing ICallbackEventHandler

Asynchronous communication with the server is also made possible by implementing the ICallbackEventHandler interface (System.Web.UI.ICallbackEventHandler). ICallbackEventHandler sends user defined elements back to the server to process and the output is return in the form of a string back to the client, thus enabling a partial postback without the UpdatePanel.

These are the steps in implementing ICallbackEventHandler interface:

Inherit ICallbackEventHandler

Inherit ICallbackEventHandler on the page or the user control where it needs to be implemented. The code for this will be

public partial class Contributions : System.Web.UI.UserControl, ICallbackEventHandler

As a result, two functions needs to be implemented in the code
  1. public void RaiseCallbackEvent(string eventArgument)
  2. public string GetCallbackResult()
RaiseCallbackEvent() is automatically called when there is a Callback event. The GetCallbackResult() is called in sequence right after the RaiseCallbackEvent() is processed.

public string GetCallbackResult()
return result;

This method here just ejects HTML code into the page to set the server datetime.

public void RaiseCallbackEvent(string eventArgument)
result = "document.getElementById('txtPercentage').innerHTML = '" + DateTime.Now.ToLongTimeString() + "'";

The method RaiseCallbackEvent() here, just passes the server datetime back to the client. This can typically be any server process and the result variable being set.


Below is the HTML for the usercontrol. The Callback, in this case will be initiated from a user control.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contributions.ascx.cs" Inherits="Contributions" %>

The above code will just create a CallbackEvent on onclick() event of the HTML button control.

Code File Wireup

The code for the server side OnPageLoad() will be,

if (!IsPostBack)
string callback = Page.ClientScript.GetCallbackEventReference(this, "arg", "DisplayResults", "context");
string script = "function CallBack(arg,context){" + callback + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CB", script, true);

GetCallbackEventReference() obtains a reference to the client function that when invoked, initiates a client callback to the server.

public string GetCallbackEventReference(
string target,
string argument,
string clientCallback,
string context,
string clientErrorCallback,
bool useAsync

target - The server control that initiates the callback. The control 'must' implement the ICallbackEventHandler interface

argument - Argument that is passed from the client script to the server for processing

clientCallback - The client script method that needs to be called after the server callback is processed

context - Client script that is evaluated on the client before initiating a callback to the server

useAsyn - When true initiates the call in the asynchronous fashion

function CallBack(arg,context) - is the client side script that invokes the GetCallbackEventReference() with the required arguments.

All this should set you going for your implementation of ICallbackEventHandler.