Client Callbacks is a new feature of ASP.NET 2 which allow client side script to call server side method asynchronously without post back to fetch new data from server and change the display in client brower dynamically.
The following procedure has to be followed to create Client Callbacks:
To receive the call back from the client the page must implement the ICallbackEventHandler interface.
public partial class ClientCallbackPage : System.Web.UI.Page, ICallbackEventHandler
The ICallbackEventHandler has two methods RaiseCallBackEvent() that receives data from the client browser as a string agrument which is triggered first and GetCallbackResult() that provides the data to the client browser and is trigger next.
In our example, we will pass the Selected Country value to the server in the RaiseCallBackEvent()
public void RaiseCallbackEvent(string callbackEventArgument)
m_callbackEventArgument = callbackEventArgument;
m_callbackEventArgument is a private string member variable and we will assign the selected country value passed from the client browser on it. Now in the GetCallbackResult() method we can retrieve the cities list for the selected country from the database or any other datastore and passed the cities list back to the client browser. Here in this example I have just hardcoded the cities list that is suppose to come from database.
public string GetCallbackResult()
string callbackResult = String.Empty;
callbackResult += "New Delhi|New
callbackResult += "Bejing|Bejing;Shanghai|Shanghai;Xian|Xian;";
function ClientCallbackScript(result, context)
var lstCity = document.forms.elements['lstCity'];
var cities = result.split(';');
for (var i = 0; i < cities.length - 1; ++i)
var properties = cities[i].split('|');
var cityName = properties;
var cityId = properties;
var option = document.createElement("option");
option.value = cityId;
option.innerHTML = cityName;
Here the result parameter holds the data that is send by the server and now this has to be parsed and filled up in the cities listbox.
Now the final step is to define the event in which the Callback trigger is fired. Here in our case we will define the Callback trigger to fire on the Country dropdown list changed event.
protected void Page_Load(object sender, EventArgs e)
if (! Page.IsPostBack)
string callbackEventReference =
ddlCountry.Attributes["onclick"] = callbackEventReference;
Below is the complete sequence diagram of the above process