ASP.NET WebForms – Use ICallbackEventHandler to lighten the load of PostBacks

Much has been written over the years regarding the drawbacks of ASP.NET and how MVC/SPAs solve many of these problems, so there’s not much to rehash there. However, if you’re stuck working with older technology, there is a handy tool to make your ASP.NET WebForms site more responsive. Implementing the ICallbackEventHandler interface in your WebForms will help do the following:

  1. Limit the amount of data that must be retrieved from the server
  2. Maintain user’s experience by eliminating the need for entire page re-rendering.

To illustrate, lets create a callback that calculates a person’s age.

  1. Create a new blank ASP.NET Web Application
  2. Add a Web Form and call it default.aspx
  3. Add “ICallbackEventHandler” to default page’s code-behind class file.
  4. There are two methods created. GetCallBackResult and RaiseCallbackEvent.
  5. Register a client script block in the Page_Load method. This script block represents the client side JavaScript that acts as the glue between the client and server.
  6. Press “Play”. When default.aspx loads in the browser, view the source html. Depending on the browser, you’ll see something similar to this:

    Notice that the Page.ClientScript.GetCallbackEventReference in our code renders a function call to “WebForm_DoCallback”. We did not create this function, .NET did, and you can see what it looks like by clicking on the script link beginning with “/WebResource.axd”. I won’t pasted the entire contents of that file, but the method signature looks like this:

    This function’s arguments align step 5’s server side Page.ClientScript.GetCallbackEventReference method:

      • eventTarget – The server Control that handles the client callback. The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent method.
      • eventArgument – An argument passed from the client script to the server RaiseCallbackEvent method.
      • eventCallback – The name of the client event handler that receives the result of the successful server event.
      • context – The client script that is evaluated on the client prior to initiating the callback. The result of the script is passed back to the client event handler.
      • errorCallback – The name of the client event handler that receives the result when an error occurs in the server event handler.
      • useAsync – true to perform the callback asynchronously; false to perform the callback synchronously.

     

  7. Here’s what the code behind looks like now:


    The two important methods are GetCallbackResult and RaiseCallbackEvent. RaiseCallbackEvent is where the client’s payload enters our code. Immediately after executing this, the GetCallbackResult function returns a string, back to the client.

    The *.aspx page looks like this:

  8. You’ll notice the there are two JavaScript function definitions and a single invocation. onCalcualteAgeSuccess and onCalculateAgeError were registered as the callback event handlers in step 5. They will wait patiently for the server to return something. The Javascript function that is being invoked is the CalculateAge function that we also defined in step 5. It is essentially a wrapper function for the native ASP.NET WebForm_DoCallback function.
  9. Press play and try both out. On my system, the callback returns 300 KB and the full PostBack returns a whopping 1700 KB, all to retrieve a few characters of data!

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *