Asynchronous Client Callbacks - ICallbackEventHandler

InterServer Web Hosting and VPS
Sony Playstation PS4 Pro Custom Build NVMe SSD Drive - Enclosure case and Crucial NVMe SSD
PS4 Pro Custom External SSD NVMe Drive

How I built a PS4 Pro Custom External SSD NVMe Drive...

NUC8i7BEH with memory
Intel NUC8I7BEH Review and Custom Build

This is my review of the Intel NUC8i7BEH and a summary of my build...

Card image cap
Asus M5A88-M EVO Review

This is my review of the Asus M5A88-M EVO motherboard...

Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit
Corsair Vengeance Blue Review

This is my review of the Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit memory...

Robotic Arm with USB PC Interface Review
Robotic Arm with USB PC Interface Review

This is my review of the Robotic Arm with USB PC Interface...

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


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.


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


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) {

<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);

Sponsored Links
SolarSystem - AngularJS Demo
SolarSystem - Angular JS Demo

Check out my first Angular JS demo hosted in Azure, it helped win my first Angular contract. Put the sound on.