ASP.NET 2.0 comes with a built-in mechanism to execute out-of-band calls to a remote serverscript callbacks. With a script callback, you can invoke a function on the server, download data to the client, and update the current page. Data is first marshaled from the client to the server and then back after some server-side code has been executed. With some client script code, you update the pages Document Object Model (DOM) and provide users with the effect of a postback but without a full-page refresh. Especially in these days of rich user pages with tons of images, graphics, and multimedia contents, being able to refresh only the updated parts of the page is a key enhancement.
Script callbacks require browsers with advanced capabilities, but 90 percent of todays browsers have these required capabilities: being able to submit out-of-band requests and a rich DOM. In addition to Internet Explorer (Version 5.0 and newer), Mozilla Firefox, Safari 1.2, and Netscape 6.0 and newer versions implement a DOM and have all required capabilities.
The script callback machinery works differently on the various browsers. Basically, it consists in the browsers ability of sending HTTP requests using an out-of-band channel. On IE, ASP.NET uses the XmlHttpRequest COM objecta component that Internet Explorer has shipped since Version 5.0. On Mozilla, ASP.NET uses the browser-specific XmlHttpRequest, object which was added to Mozilla just to match the IEs COM object back in 1999. ASP.NET provides no unified client object model to hide DOM differences. This means that while the remote calls will always work correctly on most browsers, your JavaScript client callback code may not. The reason? You have to write client code that is 100-percent compliant with the HTML DOM specification.
Script callbacks are a low-level way of accomplishing this modern form of remote scripting. Recently, a couple of frameworks for doing the same have emerged for the .NET platformAjax.NET and Atlas. An alpha version of the latter was presented at PDC 2005, and Ill cover it in greater detail in a future column. Lets tackle Ajax.NET, instead. Ajax.NET is an open-source ASP.NET library that greatly simplifies coding remote scripting.
The Ajax.NET programming model develops in three main steps. You first register an HTTP handler that will carry out most of the work. Next, you mark the ASP.NET page class and some of its server methods with a custom attribute. The custom [Ajax] attribute qualifies methods on the page class that can be called from the client through the infrastructure set up by the HTTP handler. The handler automatically serializes .NET objects to JavaScript classes so that structured data can be exchanged between the client and the server:
<script type="text/javascript"><br>function GetCustomerDetail()<br>{<br> var customerID = document.getElementById("customerID");<br> var response = TestAjax.GetCustomerByID(customerID.value);<br> var oCustomer = response.value;<br> alert(oCustomer.LastName);<br> :<br>}
The preceding code snippet shows a typical JavaScript function that is attached to a button or any other submit event. TestAjax is the name of the page class that will be invoked and GetCustomerByID is a method on this class. Both the page class and the method are flagged with the [Ajax] attribute.
With Ajax you use a different, higher-level programming style, but the tools being used under the hood are the same as with ASP.NET script callbacks.
Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant
based in Rome, Italy. Dino is a contributing editor to Windows Developer
Network and MSDN Magazine, and the author of several books for Microsoft
Press including Building Web Solutions with ASP.NET and ADO.NET
and Applied XML Programming for .NET. Contact Dino at [email protected].