One of the most overlooked features of ASP.NET 2.0, part of Visual Studio 2005 or "Whidbey", is the Client Callback feature. This feature allows you to programmatically call server-side methods through client-side JavaScript code without the need for posting back the page. This article describes how to use the Client callback feature to implement your own callback scenario and introduces the new TreeView control that has this feature built in. The code samples in this article were written using the "Whidbey" version distributed at the Professional Developer's Conference IN LATE 2003.
The need for client callbacks
Due to the nature of the stateless HTTP protocol, every time the Web user requires retrieving of data from the server or wants to invoke some code that needs to be executed on the server-side, he or she has to submit the page first. On post-back, event handlers will execute the code and serve the data back to the user whose browser has to re-render the page. This event model works fine for most of the cases, but imposes a few restrictions that ASP.NET developers have learned to live with. to keep the state of form input controls on the client, the developer either has to work with extensive ViewState information that slows down the page retrieval or has to write some complex programming logic. Secondly, re-rending the page requires some processing time by the Web browser. for small pages this is not a problem, but for a page with heavy DHTML use or simply lots of content, some flickering can occur even if you use SmartNavigation and dial-up users will notice the blue processing Bar Tha t appears at the bottom of the browser. For these reasons, the ability to call server-side methods from client-side code is a request that many Web developers have had for a long time. The good news is that ASP.NET 2.0 has THIS Feature Builtin.how Client Callback Can Be Implement Now
Before we go to ASP.NET 2.0's Client Callback feature, let's take a look at what Web developers currently do to overcome this problem. Calling server-side methods from JavaScript is something that is currently possible using Microsoft's XMLHTTP ActiveX object. This ActiveX object allows you to retrieve XML files over the Internet using the HTTP protocol However, unlike the name implies, you can use this object to issue an HTTP request to any server -. including Classic ASP, regular HTML, or even PHP files - and just . retrieve the raw HTML output Since the XMLHTTP object is pretty much a standard ActiveX object, you can instantiate it using regular JavaScript So, let's take a look at this sample code that retrieves the HTML code from Google's front page:. function RetrieveGoogleFrontPage () {VAR XMLHTTP = New ActiveXObject ("msxml2.xmlhttp.4.0"); XMLHTTP.Open ("get", "http://www.google.com", false); xmlhttp.send (); return xmlhttp.responsext; } From this code Sample, You Can See t hat using the XMLHTTP object is fairly simple. You simply specify a URL to issue the request and retrieve the complete content that is being returned from the Web server. All this is done in JavaScript, so the page on which this code resides is actually not being posted back. Also, notice that the XMLHTTP object returns the complete response text. This means that if you just want to retrieve business data from the server side, you have to write a special page that returns the business data with the unnecessary HTML code That Bloats a regular page.
ASP.NET 2.0'S Client Callback
Now, let's fast forward to ASP.NET 2.0. The new ASP.NET abstracts the use of the XMLHTTP object. Internally the Client Callback feature still uses the XMLHTTP object, but both the Web user as well as the Web developer are shielded from it . The Client Callback feature really consists of two things: the new ICallbackEventHandler interface as well as the new Page.GetCallbackEventReference method The architecture boils down to the following basic steps The Page.GetCallbackEventReference method and its overloads will create JavaScript code snippets that you.. need to place on the client side. These code snippets contain code that sends an HTTP request back to the page (using the XMLHTTP object under the hood). The request is then handled on the server side by a Web control that implements the ICallbackEventHandler interface In Most Cases, That Web Control Is The Page Specific User Controls Or Web Controls That React To The Request, As You Will See Later in This Art icle. Once the request has been handled, the result is then passed back to the client through another JavaScript function whose sole purpose is to react to the result of the request. Let's take a look at this ASP.NET 2.0 code sample that simply retrieves The Server Time and Displays It THROUGH A Regular JavaScript Alert:
<% @ page language = "c #" compilewith = "serverTime.aspx.cs" classname = "asp.servertime_aspx"%>