Use UpdatePanel [by Dorian Deng]

Source: Internet
Author: User

1 Overview

The ASP. NET UpdatePanel control allows you to create a wide range of customer-centric Web applications. With the UpdatePanel control, you can refresh the selected page part instead of refresh the entire page using a response. This is like executing a partial page update. A Web page that contains one ScriptManager and one or more UpdatePanel is automatically added with local page updates without the need to customize the client code.

1.1 scenarios

UpdatePanel is a server control that helps you develop Web pages that use complex client behavior, so that Web pages can present more interactions to users. Coordination between servers and clients to update only the specified page section usually requires a deep knowledge of ECMAScript (Javascript. However, by using the UpdatePanel control, you can add partial page updates to the page without writing any client script. If you want to, you can add custom client scripts to enhance the user experience of the client. When the UpdatePanel control is used, the page behavior is independent of the browser, and the data transmission volume of the client and server is potentially reduced.

1.2 Background

UpdatePanel is used to specify a region that can be updated without refreshing the entire page. This process is coordinated by the ScriptManager Server Control and the client PageRequestManager class. When a local update is available, the control can be asynchronously sent to the server. Asynchronous sending is the same as normal sending. The result Server Page executes the complete page and control lifecycle. However, with asynchronous sending back, page update is limited to the area of the page marked as updated in the UpdatePanel control. The server only sends HTML tags to the affected elements in the browser. In the browser, the client PageRequestManager class executes the Document Object Model (DOM) operation to replace the existing HTML with the updated tag. The following example shows the first loading of the page and the asynchronous sending of the content of the UpdatePanel refresh control.

2 Use of UpdatePanel

The basic usage of UpdatePanel is very simple. You can add a SciprtManager control and one or more UpdatePanel controls to the page, then, place the page elements to be partially updated and the control that triggers the update to the ContentTemplate attribute of the UpdatePanel control. As shown in the following code, a Button control is added to it:

<Asp: UpdatePanelID = "UpdatePanel1"
UpdateMode = "Conditional"
Runat = "server">
<ContentTemplate>
<Asp: Button ID = "Button1"
Text = "RefreshPanel"
Runat = "server"/>
</ContentTemplate>
</Asp: UpdatePanel> to use the UpdatePanel Control for availability, you must set the EnablePartialRendering attribute of ScriptManager to true.
The UpdatePanel control can output <div> or <span> elements to form a Block or Inline area on the page. You can set the RenderMode attribute to Block (default, <div>) or Inline (<span>.

2.1 content of UpdatePanel

You can use the ContentTemplate attribute to declare the content or add the content to the UpdatePanel control in the designer. In the tag, this attribute is displayed as the <ContentTemplate> element. To add content programmatically, you can use the ContentTemplateContainer attribute.
When a page that contains one or more UpdatePanel controls is output for the first time, all content in the UpdatePanel control is output and sent to the browser. In subsequent asynchronous updates, the content in a single UpdatePanel control may be updated. Update the settings that depend on the panel, the elements that cause sending back, and the code assigned to each panel.

2.2 specify the UpdatePanel trigger

By default, any control in the UpdatePanel control will send back asynchronously and refresh the panel content. However, you can also configure other controls on the page that are not in the panel to refresh the UpdatePanel control. You can define a trigger for the UpdatePanel control. The trigger specifies the sender control and event to bind the Panel update. When an event specified by the trigger control is triggered (such as a button Click event), the update panel is refreshed.
The following example shows how to add a trigger to the UpdatePanel panel.

<Asp: Button ID = "Button1"
Text = "Refresh Panel"
Runat = "server"/>
<Asp: ScriptManager ID = "ScriptManager1"
Runat = "server"/>
<Asp: UpdatePanel ID = "UpdatePanel1" UpdateMode = "Conditional" runat = "server">
<Triggers>
<Asp: AsyncPostBackTrigger ControlID = "Button1"/>
</Triggers>
<ContentTemplate>
<Fieldset>
<Legend> UpdatePanel content </legend>
<% = DateTime. Now. ToString () %>
</Fieldset>
</ContentTemplate>
</Asp: UpdatePanel>

The trigger is defined by the <asp: AsyncPostBackTrigger> element in the <Triggers> element of the UpdatePanel control. (If you are editing the page in Visual Studio, you can click the ellipsis next to the Triggers attribute in the UpdatePanel attribute panel to open an UpdatePanelTrigger Set Editor dialog box to create a trigger .) The required attribute of a trigger is ControlID, which is used to specify the ID of the control that can cause panel update.
In the preceding example, a button is not declared in the panel, but is specified as a trigger in the Panel. When a button event is triggered, the same result is returned when the Panel is included, that is, the Panel is updated.
The event of the trigger control is optional. If no event is specified, the trigger uses the default event of the control. For example, for the Button control, the default event is the Click event.

2.3 use UpdatePanel on the master page

To use the UpdatePanel control on the master page, you must determine how to use the ScriptManager control. If a ScriptManater control is placed on the master page, the ScriptManager control can play a role in all content. (If you want to declare a script or service on the content page, you can add a ScriptManagerProxy on the page, which has the same attributes and methods as ScriptManager .)
If the master page does not contain the ScriptManager control, you must place a ScriptManager control on each content page containing the UpdatePanel control, the selection of the design depends on how client scripts are managed in the application.
If the master page contains the ScriptManager control and you do not want to use the partial page output function on a content page, you must set the EnablePartialRendering of the ScriptManager control in the program to false.

2.4 use nested UpdatePanel controls

The UpdatePanel control can be nested. If the parent panel is refreshed, all nested panels are refreshed.
The following code defines another UpdatePanel control in an UpdatePanel control.

<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> UpdatePanelUpdateMode example </title>
<Style type = "text/css">
Div. NestedPanel
{
Position: relative;
Margin: 2% 5% 2% 5%;
}
</Style>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: ScriptManager ID = "ScriptManager"
Runat = "server"/>
<Asp: UpdatePanel ID = "OuterPanel"
UpdateMode = "Conditional"
Runat = "server">
<ContentTemplate>
<Div>
<Fieldset>
<Legend> outer Panel </legend>
<Br/>
<Asp: Button ID = "OPButton1"
Text = "outer panel button"
Runat = "server"/>
<Br/>
The last update is in:
<% = DateTime. Now. ToString () %>
<Br/>
<Br/>
<Asp: UpdatePanel ID = "NestedPanel1"
UpdateMode = "Conditional"
Runat = "server">
<ContentTemplate>
<Div class = "NestedPanel">
<Fieldset>
<Legend> nested panel </legend>
<Br/>
The last update is in:
<% = DateTime. Now. ToString () %>
<Br/>
<Asp: Button ID = "NPButton1"
Text = "nested panel buttons"
Runat = "server"/>
</Fieldset>
</Div>
</ContentTemplate>
</Asp: UpdatePanel>
</Fieldset>
</Div>
</ContentTemplate>
</Asp: UpdatePanel>
</Div>
</Form>
</Body>
</Html>

2.5 create an UpdatePanel control with a program

To Add an UpdatePanel Control to the page, you can create a new UpdatePanel instance and use the Add (Control) method of its ContentTemplateContainer attribute to Add other controls. You cannot directly use the ContentTemplate attribute to add controls.
If the UpdatePanel control is added by a program, you can use the Panel trigger only when the control from the same named container such as UpdatePanel control is sent back.
The following code demonstrates how to use a program to add the UpdatePanel control.

<% @ Page Language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
UpdatePanel up1 = new UpdatePanel ();
Up1.ID = "UpdatePanel1 ";
Up1.UpdateMode = UpdatePanelUpdateMode. Conditional;
Button button1 = new Button ();
Button1.ID = "Button1 ";
Button1.Text = "Submit ";
Button1.Click + = new EventHandler (Button_Click );
Label label1 = new Label ();
Label1.ID = "Label1 ";
Label1.Text = "A full page postback occurred .";
Up1.ContentTemplateContainer. Controls. Add (button1 );
Up1.ContentTemplateContainer. Controls. Add (label1 );
Page. Form. Controls. Add (up1 );

}
Protected void Button_Click (object sender, EventArgs e)
{
(Label) Page. FindControl ("Label1"). Text = "Panel refreshed at" +
DateTime. Now. ToString ();
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> UpdatePanel Added Programmatically Example </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: ScriptManager ID = "TheScriptManager"
Runat = "server"/>
</Div>
</Form>
</Body>
</Html>

3 key attributes of UpdatePanel

ChildrenAsTriggers: Indicates whether to update the panel content for sending back of the direct sub-control from the UpdatePanel control. Update when set to true. Otherwise, update is not performed. The default value is true. If this attribute is set to false, the UpdateMode of the UpdatePanel control must be set to Conditional. Otherwise, an InvalidOperationException is thrown.

UpdateMode: indicates when to update the panel. When an UpdatePanel control is not included in another UpatePanel control, panel updates are performed based on the UpdateMode, ChildrenAsTriggers attribute settings, and trigger set. When an UpdatePanel control is inside another UpdatePanel control, the Child panel is automatically updated on the parent panel.
The content of the UpdatePanel control is updated in the following situations:

  • If the UpdateMode attribute is set to Alwarys, the content in the UpdatePanel control is updated at each sending time from any place on the page. This includes sending back from the controls contained in other UpdatePanel controls and from the UpdatePanel controls.
  • If the UpdatePanel control is nested in another UpdatePanel control, it will also be updated when the parent panel is updated.
  • If the UpdateMode attribute is set to Conditional and the following conditions appear:
    • Call the Update () method of the UpdatePanel control explicitly.
    • This event is caused by the trigger control defined by the Triggers attribute in the UpdatePanel control. In this case, the control will explicitly trigger updates to the panel content. Controls defined as triggers can be internal or external to the UpdatePanel control.
    • The ChildrenAsTriggers attribute is set to true and is returned because of the Child control in the UpdatePanel control. Child controls in nested UpdatePanel controls do not cause updates to the outer UpdatePanel control, unless they are defined as triggers.

4. Summary
From the above content, we can see that using the UpdatePanel control can help you easily develop ASP. NET Applications with AJAX features. Of course, it is not omnipotent, excessive use will cause a certain amount of performance overhead, at the same time it is still with the current part of ASP. NET controls are not compatible, such as TreeView, Menu, and WebParts controls.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.