Implementing ICallBackEventHandler in ASP.Net Application


Hi Friends, in this article I will be explaining How to Implement ICallBackEventHandler in application. We can run server side code from the client without performing a Postback using ICallBackEventHandler interface. Before starting it we will see how Postback feature works. When any event is triggered on page, a HTTP post request will be sent to the server, and request run through a series of page events ( Including loading the viewstate and rendering controls over the page. Where as in Client side callback whenever event is triggered then event will be posted to a javascript eventhandler and the eventhandler sends asynchronous request to the server. Request runs through events but rendering of the page will not happen again. Once information is loaded, the result of the callback will be sent to the script and then the script sends data to the web page without refreshing the page.

Let’s create a simple example

To use ICallbackEventHandler, we will need to inherit it on the page.

public partial class _Default : System.Web.UI.Page, ICallbackEventHandler

ICallbackEventHandler interface has two functions which we need to implement.

  1. public void RaiseCallbackEvent(string eventArgument)
  2. public string GetCallbackResult()

Method RaiseCallbackEvent gets called automatically whenever callback event is raised and GetCallbackResult returns the string value to client.

In page load event we have to create CallBackeventReference which will be interacting with client and server.

The class looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace MSCoderICallBackEventHandlerSample
    public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
        protected void Page_Load(object sender, EventArgs e)
            ClientScriptManager scriptManager = Page.ClientScript;

            String cbReference = scriptManager.GetCallbackEventReference(this, "returnValue", "GetResult", "context");

            String callbackScript = "function CallServer(returnValue, context) {" + cbReference + "; }";

            scriptManager.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
            if (Page.IsCallback)
                ////IF any operation do it here.

        #region ICallbackEventHandler Members

        private string returnValue;
        public string GetCallbackResult()
            return returnValue;

        public void RaiseCallbackEvent(string eventArgument)
            returnValue = DateTime.Now.ToString();


HTML of the example –

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MSCoderICallBackEventHandlerSample._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>ICallbackEventHandler Sample</title>

    <script language="javascript" type="text/javascript">
        function GetResult(returnValue, context) {
            document.getElementById("txtDateTime").value = returnValue;
        function SendRequest() {
            return false;

    <form id="form1" runat="server">
    <asp:TextBox ID="txtDateTime" runat="server" Text="">
    <asp:Button ID="btnDateTime" OnClientClick="return SendRequest()" runat="server"
        Text="Get Server Date Time" />


Download Code

That’s all.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s