AJAX Matters

  • Home
  • Articles
  • About Us
Rss Feeds

Article Topics

All Articles
General AJAX
ASP.NET AJAX
Google Web Toolkit
AJAX Using Java
AJAX Using PHP
Dojo
 

articles >> asp.net ajax >> Using ICALLBACKEventHandler in ASP.NET

Using ICALLBACKEventHandler in ASP.NET

By : Ashish Sarda
Jul 13, 2007
Printer friendly

About ICallbackEventHandler:

ASP.NET 2.0 introduced an interface named ICallbackEventHandler (System.Web.UI.ICallbackEventHandler) to allow asynchronous communication with the server. Unlike Postback, in Callback only user defined information is sent to the server. Instead of using Postback to post the page, ICallbackEventHandler uses the DoCallback event to send user defined data to server, and return a String to client; on the client-side JavaScript can then manipulate the string. In total we have to use four functions for the implementating ICallbackEventHandler; two client side functions (in javascript) and two server side functions (C# in this case).


Use of ICallbackEventHandler:


To use ICallbackEventHandler, we will need to inherit it on the page or in a user control. The code will for this will be:

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

As a result of inheriting from ICallbackEventHandler, we have to implement two functions, namely:

  • public void RaiseCallbackEvent(String eventArgument)
  • public String GetCallbackResult()

As name of the above two functions indicates, the first function gets called automatically whenever there is a CallbackEvent. After the first function the second function i.e. GetCallbackResult gets called and returns a string to the client

So how to raise a CallbackEvent? For this we will have to use javascript. There will be two javascript functions:

  1. A function which will call RaiseCallbackEvent.
  2. A function which will handle the response from the server. This function will be called automatically after GetCallbackResult() and the string returned by GetCallbackResult will appear in JavaScript as input to this function.

Implementing ICallbackEventHandler

1. Create an ASPX page as follows

<body>
<form id="form1" runat="server">
<div>
Select The Item and Click the Button
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<br />
<br />
<button type="Button" onclick="CheckForTimeZone()">Get Timezone</button>
<br />
<table><tr>
<td>GMT Time zone is :</td>
<td><div id="Results" runat="server"></div></td>
</tr></table>
<br />
</div>
</form>
</body>

In above code “CheckForTimeZone()” in the button tag is a javascript function which we will add later. Also note that the button is a simple HTML control.

2. JavaScript in the Aspx page.

  <script type="text/ecmascript">

    function CheckForTimeZone()
{
var lb = document.getElementById("DropDownList1");
var selectedItem = lb.options[lb.selectedIndex].text;
CallServer(selectedItem, "");
}

function ReceiveServerData(retValue)
{  
document.getElementById("Results").innerHTML = retValue;
}

  </script>

In above code  CallServer(selectedItem, "") will be responsible for calling RaiseCallbackEvent on server side. And the ReceiveServerData(retValue) function gets called by GetCallbackResult().
To make above code work the way we want; we will need to add some code to the aspx.cs file.


    Next Page>>    








General AJAX | ASP.NET AJAX | Google Web Toolkit | AJAX Using Java | AJAX Using PHP | Dojo


© 2000 - 2008 vDerivatives Limited All Rights Reserved.