Asynchronous Client Callbacks - ICallbackEventHandler


Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

Asynchronous Client Callbacks - ICallbackEventHandler

Here we look at how to make Asynchronous Client Callbacks using the ICallbackEventHandler interface.

Although there are other ways to achieve this such as having a web service called by jQuery or ASP.NET AJAX, once I'd understood this methodology I realised it would have come in quite handy in several scenarios where I need to pass information between client to the page and the page to the client.


Implement the interface ICallbackEventHandler
Implement GetCallbackResult
Implement RaiseCallbackEvent
On the client write a Javascript function to receive callback from server
Add server code to generate javascript code which will call the server


ICallbackEventHandler

In the page class, we need to make it implement the interface ICallbackEventHandler. This means that we have to implement it's two methods, GetCallbackResult and RaiseCallbackEvent.

RaiseCallbackEvent

This is the method which gets called by the client, and it can receive parameters passed to it to from the client.

GetCallbackResult

This method returns a string back to the client.

Asynchronous Client Callbacks - ICallbackEventHandler Example

Here is my working example which has an ASP.NET button which actually calls a Javascript method which was generated using the below C# code. The Javascript method which was generated in the C# then calls to the server passing in the number 100., which takes a number (well string is passed through) and calculates the area of a circle using that number, and calls the RaiseCallbackEvent method. Now execution begins on the server and we do our C# stuff we want to do. Once RaiseCallbackEvent has run then next GetCallbackResult executes and responds back to the client with our calculated circle area value, where a Javascript method called DisplayArea receives the value from the server and uses it on the client.


<script type="text/javascript">
    function DisplayArea(area) {
        alert(area);
    }
</script>

<asp:Button ID="ButtonASP" runat="server" OnClientClick="CallServerForCircleArea(100);" Text="Click Here" />



namespace AsynchronousClientCallback
{
    public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
    {
        Int32 myNumber = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            String callbackRef = Page.ClientScript.GetCallbackEventReference(this, "area", "DisplayArea", "");
            String callbackScript = "function CallServerForCircleArea(area) {" + callbackRef + "; }";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServerForCircleArea", callbackScript, true);
        }

        public string GetCallbackResult()
        {
            return (myNumber * myNumber * Math.PI).ToString();
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            myNumber = Convert.ToInt32(eventArgument);
        }
    }
}