Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

An Overview of Ajax.NET


An Overview of Ajax.NET

ASP.NET 2.0 comes with a built-in mechanism to execute out-of-band calls to a remote server—script 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 page’s 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 today’s 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 browser’s ability of sending HTTP requests using an out-of-band channel. On IE, ASP.NET uses the XmlHttpRequest COM object—a 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 IE’s 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 platform—Ajax.NET and Atlas. An alpha version of the latter was presented at PDC 2005, and I’ll cover it in greater detail in a future column. Let’s 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].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.