Using the ASP.NET UpdatePanel Control with Data-Bound Controls
You can create a richer user experience in your AJAX-enabled ASP.NET 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:
You can enable partial-page rendering by using the UpdatePanel and ScriptManager Web server controls. The UpdatePanel control identifies a region of a page that can be updated. The ScriptManager control keeps track of the UpdatePanel controls that are on a page and the controls that trigger the UpdatePanel controls to refresh. Controls inside an UpdatePanel control that cause a postback are automatically identified as triggers for the update panel. You can also specify controls that are controls outside an UpdatePanel control. External triggers can be child controls of another UpdatePanel control. For more information, see Partial-Page Rendering Overview and UpdatePanel Control Overview.
This topic contains the following:
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 refreshes the page, even if the trigger was not for that UpdatePanel control. To make sure that the UpdatePanel control is refreshed 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 Web controls that accept user input, and the other control displays what the user enters. 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 UpdatePanel controls are refreshed.
You can also nest an UpdatePanel control inside another UpdatePanel control. If you set the UpdateMode property of both the outer and nested controls to Conditional, the outer panel is not refreshed when only the inner panel is triggered. However, if a trigger refreshes the outer panel, both the outer and inner panels are refreshed.
The following example includes a GridView control inside an UpdatePanel control. Each row of the GridView control contains a nested GridView control that is inside a child UpdatePanel control. When a new page of records is requested from an inner GridView control, 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 of the nested panels are all refreshed.
Controls inside 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 refresh must be registered with the ScriptManager control on the page. This occurs automatically for the child controls in an UpdatePanel control. You can specify triggers declaratively using the Triggers collection of an UpdatePanel control. Additionally, you can programmatically identify triggers and use server code to cause an UpdatePanel control to refresh.
In cases where a trigger control is not available at design time, you can register the control as a trigger by using the RegisterAsyncPostBackControl method of the ScriptManager control. Controls that are programmatically identified as triggers must be registered every time that a postback occurs. We recommend that you put calls to the RegisterAsyncPostBackControl method in the Page_Load event for your page, as shown in the following example:
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.
The following example shows a page that registers a control as a trigger by using the RegisterAsyncPostBackControl method. It then programmatically refreshes an UpdatePanel control by using the Update method.
You can add an UpdatePanel to a user control or to a custom control. However, the page that contains your control might not include the necessary ScriptManager control with its EnablePartialRendering property set to true. Therefore, in your custom control you can determine whether partial-page rendering is enabled by calling the static GetCurrent method of the ScriptManager control. If there is no ScriptManager control on the page, the GetCurrent method returns null. Otherwise, you can check the value of the EnablePartialRendering property of the ScriptManager control and include an UpdatePanel control if the EnablePartialRendering property returns true.
The following example shows the CreateChildControls method of a custom control that inherits from the CompositeControl class. If partial-page rendering is enabled for the page, the custom control puts its contents in an UpdatePanel control.
The following example shows a custom control that includes an UpdatePanel control if partial-page rendering is enabled.