Using the ASP.NET UpdatePanel Control with Data-Bound Controls

Introduction

You can create a richer user experience in your Microsoft ASP.NET AJAX applications by using partial-page rendering. Partial-page rendering removes the need for the whole page to refresh as the result of a postback. Instead, you can specify only the region of the page to be refreshed. As a result, users no longer see the whole page reload with every postback.

This topic assumes that you are familiar with the UpdatePanel control. If not, review the following topics:

There is no limit to the number of UpdatePanel controls that you can put on a page. As a result, you can specify regions of a page that are refreshed independently of the whole page and independently of each other.

By default, the UpdateMode property of an UpdatePanel control is set to Always. This means that whenever a partial-page refresh is triggered, the UpdatePanel control will refresh the page region regardless of whether it was the UpdatePanel control that was triggered. To make sure that the UpdatePanel control will refresh only when it has been triggered, you can set the UpdateMode property of the UpdatePanel control to Conditional.

The following example includes two UpdatePanel controls: one control includes controls to accept user input, and the other control displays a summary of the input. Each UpdatePanel control has its UpdateMode property set to Conditional. As a result, if a user clicks the Cancel button to clear the fields of the input form, only the input form of the UpdatePanel control is refreshed. If a user clicks the Insert button to submit the form, both of the UpdatePanel controls are refreshed.

You can also nest an UpdatePanel control in the ContentTemplate template of another UpdatePanel control. If you set the UpdateMode property of both the outer and nested controls to Conditional, the outer panel will not refresh its page region if only the inner panel has been triggered. However, if a refresh of the outer panel is triggered, both the outer and inner panels will refresh their page regions.

The following example includes a GridView control inside an UpdatePanel control. For each row of the GridView control, there is a nested GridView control that is inside a child UpdatePanel control. When a new page of records is requested from an inner GridView, the page regions of the outer panel and the panels in the other rows of the outer GridView control are not refreshed. When the outer GridView control displays a new page of records, the page regions of the outer panel and the nested panels are all refreshed.

Controls in the content template for an UpdatePanel control that cause a postback are automatically configured as triggers for that UpdatePanel control. However, you might want to disable the automatic triggers and trigger a refresh of the UpdatePanel control from an external control only. To do this, set the ChildrenAsTriggers property of the UpdatePanel control to false. Then set the UpdateMode property of the UpdatePanel control to Conditional. These property settings will cause a refresh of the panel only if triggered an external control or by a call to the Update() method.

The following example displays the categories, subcategories, and names of products from the AdventureWorks sample database. The category list rarely changes and there is no need to refresh the list of categories every time that a list of subcategories or products is displayed. Therefore, in the UpdatePanel control that contains the category list, you can set the ChildrenAsTriggers property to false and the UpdateMode property to Conditional. This makes sure that the UpdatePanel control refreshes only when explicitly requested.

Controls that trigger an UpdatePanel control to refresh are registered with the ScriptManager control on the page. This occurs automatically for the child controls in an UpdatePanel control's ContentTemplate. You can specify triggers declaratively using the Triggers collection of an UpdatePanel control. Additionally, you can programmatically identify triggers and cause an UpdatePanel control to refresh in the server code of your page.

In cases where a trigger control is not available at design time, you can register the control as a trigger by using the RegisterAsyncPostBackControl(Control) method of the ScriptManager control. Controls that are programmatically identified as triggers must be registered whenever a postback occurs. We recommend that you put calls to the RegisterAsyncPostBackControl(Control) method in the Page_Load event for your page, as shown in the following example.

CS

vb

To programmatically refresh the UpdatePanel control, you can call the Update() method of an UpdatePanel control. This is useful if you must perform some processing on the server before refreshing the UpdatePanel control. The following example shows how to refresh an UpdatePanel programmatically.

App_Code

protected override void CreateChildControls() {
base.CreateChildControls();
Control parent;
Control container;
// Get a reference to the ScriptManager object for the page
// if one exists.
ScriptManager sm = ScriptManager.GetCurrent(Page);
if (sm == null || !sm.EnablePartialRendering)
{
// If partial rendering is not enabled, set the parent
// and container as a basic control.
container = new Control();
parent = container;
}
else
{
// If partial rendering is enabled, set the parent as
// a new UpdatePanel object and the container to the
// content template of the UpdatePanel object.
UpdatePanel up = new UpdatePanel();
container = up.ContentTemplateContainer;
parent = up;
}
AddDataboundControls(container);
Controls.Add(parent);
}

App_Code

Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
Dim parent As Control
Dim container As Control
' Get a reference to the ScriptManager object for the page
' if one exists.
Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)
If sm Is Nothing OrElse Not sm.EnablePartialRendering Then
' If partial rendering is not enabled, set the parent
' and container as a basic control.
container = New Control()
parent = container
Else
' If partial rendering is enabled, set the parent as
' a new UpdatePanel object and the container to the
' content template of the UpdatePanel object.
Dim up As UpdatePanel = New UpdatePanel()
container = up.ContentTemplateContainer
parent = up
End If
AddDataboundControls(container)
Controls.Add(parent)
End Sub

The following example shows a custom control that includes an UpdatePanel control if partial-page rendering is enabled.