Documentation for: ASP.NET Ajax Version 1.0

This documentation is for a previous version. For the current released version, see the ASP.NET Ajax documentation on MSDN.

 

 

 

 

 

 

 

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 AJAX enables you to add partial-page rendering to new or existing ASP.NET Web pages without writing client script.

This topic contains the following sections:

Scenarios

ASP.NET AJAX enables you to extend existing ASP.NET 2.0 applications and to develop new ones that incorporate AJAX (Asynchronous JavaScript and XML) functionality. Use ASP.NET AJAX 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 AJAX 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 AJAX server controls. However, you can use the APIs exposed in the client library for additional AJAX functionality.

The primary features of ASP.NET AJAX that support 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 AJAX 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 concurrent asynchronous postbacks 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.

Background

Typical Web pages built with ASP.NET Web server controls perform postbacks initiated by a user action on the page, such as clicking a button. In the response, the server renders a new page. Frequently this re-renders controls and text that did not change between postbacks.

With ASP.NET AJAX partial-page rendering, you can refresh individual regions of the page asynchronously and make the page more responsive to the user. You can implement partial-page rendering using ASP.NET Web server controls, and optionally write client script that uses the APIs in the Microsoft AJAX Library.

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. For example, in Visual Studio you can drag it from the toolbox to the Web page, or you can add it by using declarative markup in the page. The following example shows the markup for an UpdatePanel control.

CS

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <!-- Place updatable markup and controls here. -->
    </ContentTemplate>
</asp:UpdatePanel>

VB

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <!-- Place updatable markup and controls here. -->
    </ContentTemplate>
</asp:UpdatePanel>

By default, postbacks that originate 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 update panel 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. For more information about triggers, see Creating a Simple ASP.NET Page with Multiple UpdatePanel Controls.

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 the UpdatePanel 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 keeps track of all the update panels on the page and of their triggers. It coordinates partial-page rendering behavior on the server, and determines which sections of the page to render as a result of an asynchronous postback.

The following example shows an UpdatePanel control whose content is refreshed whenever a postback originates from inside the panel.

For more examples of how to use UpdatePanel controls to enable partial-page rendering, see the topics listed in the Code Examples section.

Using Client Script for Partial-Page Updates

The ECMAScript (JavaScript) PageRequestManager class in the Microsoft AJAX Library supports partial-page updates. It runs in the browser to manage the response to asynchronous postbacks and to update content in individual regions. You do not have to do anything to enable this functionality. It occurs automatically when you add one or more UpdatePanel controls and a ScriptManager control to the page.

You can also use JavaScript and the PageRequestManager class to customize partial-page updates on a page. For example, you can write script to give precedence to a specific asynchronous postback if more than one is underway. You can also enable users to cancel postbacks that are in progress.

The following example shows client script that provides an event handler that is called when the page finishes loading.

CS

<script type="text/javascript" language="javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(PageLoadedEventHandler);
function PageLoadedEventHandler() {
   // custom script
}
</script>

VB

<script type="text/javascript" language="javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(PageLoadedEventHandler);
function PageLoadedEventHandler() {
   // custom script
}
</script>

For more information about how to use the Microsoft AJAX Library for partial-page rendering, see Working with PageRequestManager Events and PageRequestManager Class Overview.

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-page 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.

note

ASP.NET Web pages that are configured for legacy rendering are not supported in ASP.NET AJAX. For more information, see ASP.NET and XHTML.

Code Examples

The following example shows partial-page rendering in action. There are two UpdatePanel controls. One control takes user input and another displays a summary of the input.

How-to and Walkthrough Topics

Class Reference

The following table lists the key server classes for partial-page rendering.

Class

Description

UpdatePanel

Specifies regions of the page to be updated during partial-page rendering.

ScriptManager

Manages ASP.NET AJAX components, partial-page rendering, client requests, and server responses on ASP.NET Web pages.

ScriptManagerProxy

Enables nested components to add script and service references to pages that already contain a ScriptManager control in a parent element.

The following table lists the key client class for partial-page rendering.

Class

Description

PageRequestManager Class

Manages client partial-page rendering and exposes members for custom client scripting.