Home > Sample chapters

Understanding AJAX and ASP.NET

ASP.NET Server-Side Support for AJAX

Much of ASP.NET support for AJAX resides in a collection of server-side controls responsible for rendering AJAX-style output to the browser. Recall from Chapter 3, “The Page Rendering Model,” that the entire page-rendering process of an ASP.NET application is broken down into little bite-sized chunks. Each individual bit of rendering is handled by a class derived from System.Web.UI.Control. The entire job of a server-side control is to render output that places HTML elements in the output stream so that they appear correctly in the browser. For example, ListBox controls render a <select/> tag. TextBox controls render an <input type=“text” /> tag. ASP.NET AJAX server-side controls render AJAX-style script and HTML to the browser.

ASP.NET AJAX support consists of these server-side controls along with client code scripts that integrate to produce AJAX-like behavior. The following subsections describe the most frequently used AJAX-oriented ASP.NET server controls: ScriptManager, ScriptManagerProxy, UpdatePanel, UpdateProgress, and Timer.

ScriptManager Control

The ScriptManager control manages script resources for the page. The ScriptManager control’s primary action is to register the AJAX Library script with the page so that the client script can use type system extensions. The ScriptManager also makes possible partial-page rendering and supports localization as well as custom user scripts. The ScriptManager assists with out-of-band calls back to the server. Any ASP.NET site wishing to use AJAX must include an instance of the ScriptManager control on any page using AJAX functionality.

ScriptManagerProxy Control

Scripts on a Web page often require a bit of special handling in terms of how the server renders them. Typically, the page uses a ScriptManager control to organize the scripts at the page level. Nested components such as content pages and user controls require the ScriptManagerProxy to manage script and service references to pages that already have a ScriptManager control.

This is most notable in the case of master pages. The master page typically houses the ScriptManager control. However, ASP.NET throws an exception if a second instance of ScriptManager is found in a given page. So, what would content pages do if they needed to access the ScriptManager control that the master page contains? The answer is that the content page should house the ScriptManagerProxy control and work with the true ScriptManager control through the proxy. Of course, as mentioned, this also applies to user controls as well.

UpdatePanel Control

The UpdatePanel control supports partial-page updates by tying together specific server-side controls and events that cause them to render. The UpdatePanel control causes only selected parts of the page to be refreshed instead of the whole page (as happens during a typical HTTP postback).

UpdateProgress Control

The UpdateProgress control coordinates status information about partial-page updates as they occur in UpdatePanel controls. The UpdateProgress control supports intermediate feedback for long-running operations.

Timer Control

The Timer control issues postbacks at defined intervals. Although the Timer control will perform a typical postback (posting the whole page), it is especially useful when coordinated with the UpdatePanel control to perform periodic partial-page updates.