Saturday, December 16, 2006

ASP.NET AJAX Partial-Page Rendering Overview

Introduction

Partial-page rendering removes the need for the whole page to be refreshed as the result of a postback. Instead, only individual regions of the page that have changed are updated. As a result, users do not see the whole page reload with every postback, which makes user interaction with the Web page more seamless. Microsoft ASP.NET 2.0 AJAX Extensions enables you to add partial-page rendering to new or existing ASP.NET Web pages without writing client script.

Scenarios

ASP.NET 2.0 AJAX Extensions enables you to extend existing ASP.NET 2.0 applications and develop new ones that incorporate AJAX (Asynchronous JavaScript and XML) functionality. Use ASP.NET 2.0 AJAX Extensions when you want to do the following:

  • Improve the user experience with Web pages that are richer, that are more responsive to user actions, and that behave like traditional client applications.
  • Reduce full-page refreshes and avoid page flicker.
  • Enable cross-browser compatibility without writing client script.
  • Perform AJAX-style client/server communication without writing client script.
  • Use the controls and components from the ASP.NET AJAX Control Toolkit.
  • Develop custom Microsoft ASP.NET AJAX controls.

Partial-Page Rendering Features

Partial-page rendering relies on server controls in ASP.NET 2.0 AJAX Extensions and on client functions in the Microsoft AJAX Library. You do not have to use the Microsoft AJAX Library to enable partial-page rendering, because this functionality is provided automatically when you use the ASP.NET 2.0 AJAX Extensions server controls.

The primary features of Microsoft ASP.NET AJAX partial-page rendering include the following:

  • A declarative model that works like ASP.NET server controls. In many scenarios, you can specify partial-page rendering using only declarative markup.
  • Server controls that perform the underlying tasks required for partial-page updates. These include the ScriptManager control and the UpdatePanel control.
  • Integration between ASP.NET 2.0 AJAX Extensions server controls and the Microsoft AJAX Library for common tasks. These tasks include enabling users to cancel a postback, displaying custom progress messages during an asynchronous postback, and determining how multiple clicks are processed.
  • Error-handling options for partial-page rendering, which enable you to customize how errors are displayed in the browser.
  • Cross browser compatibility, which is built into the Microsoft AJAX Library. Simply using the server controls automatically invokes the correct browser functionality.

Server Controls for Partial-Page Updates

To add AJAX functionality to ASP.NET Web pages, you identify individual sections of the page that you want to update. You then put the content of these sections into UpdatePanel controls. The contents of an UpdatePanel control can be HTML or other ASP.NET controls. You can add an UpdatePanel control to the page as you would any other control.

By default, postbacks originating from controls inside the update panel (child controls) automatically initiate asynchronous postbacks and cause a partial-page update. You can also specify that controls outside the UpdatePanel cause an asynchronous postback and that they refresh the UpdatePanel control's content. A control that causes an asynchronous postback is referred to as a trigger.

An asynchronous postback behaves much like a synchronous postback. All the server page life-cycle events occur and view state and form data are preserved. However, in the rendering phase, only the contents of theUpdatePanel control are sent to the browser. The rest of the page remains unchanged.

To support partial-page rendering, you must put a ScriptManager control on the page.
The ScriptManager control:

  1. keeps track of all the update panels on the page and their triggers.
  2. coordinates partial-page rendering behavior on the server.
  3. determines which sections of the page to render as a result of an asynchronous postback.

Enabling Partial-Page Rendering Support

You enable or disable partial-page rendering for a page by setting the EnablePartialRendering property of the ScriptManager control.
You can also specify whether partial-page rendering is supported for a page by setting the SupportsPartialRendering property of the ScriptManager control.
If you do not set the SupportsPartialRendering property and if the EnablePartialRendering property is true (which is the default), the capabilities of the browser are used to determine whether partial-rendering is
supported.

If partial-page rendering is not enabled for a page, if it has been disabled, or if it is not supported in a browser, the page uses fallback behavior. Actions that ordinarily would perform an asynchronous postback instead perform a synchronous postback and update the whole page. Any UpdatePanel controls on the page are ignored, and their contents are rendered as if they were not inside an UpdatePanel control.