15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1230 ❘ ChaPTer 41 Asp.net feAtures<br />

Multiple UpdatePanel Controls on a Single Web Page<br />

You can include any number of UpdatePanel controls on a page. If a postback is caused by a control that<br />

is contained in the of any UpdatePanel on the page, a partial-page postback will<br />

occur instead of a full-page postback. This will cause all the UpdatePanel controls to update according to<br />

the value of their UpdateMode property. The default value of this property is Always, which means that the<br />

UpdatePanel will update for a partial-page postback operation on the page, even if this operation occurs<br />

in a different UpdatePanel control. If you set this property to Conditional, the UpdatePanel updates<br />

only when a control that it contains causes a partial-page postback or when a trigger that you have defined<br />

occurs. Triggers are covered shortly.<br />

If you have set UpdateMode to Conditional, you can also set the ChildrenAsTriggers property to<br />

false to prevent controls that are contained by the UpdatePanel from triggering an update of the panel.<br />

Note, though, that in this case these controls still trigger a partial-page update, which may result in other<br />

UpdatePanel controls on the page being updated. For example, this will update controls that have an<br />

UpdateMode property value of Always. This is illustrated in the following code:<br />

<br />

<br />

<br />

Panel 1 render time: <br />

<br />

<br />

<br />

<br />

Panel 2 render time: <br />

<br />

<br />

Page render time: <br />

In this code, the UpdatePanel2 control has an UpdateMode property of Always; the default value. When the<br />

button is clicked, it will cause a partial-page postback, but only UpdatePanel2 will be updated. Visually,<br />

you will notice that only the “Panel 2 render time” label is updated.<br />

Server-Side UpdatePanel Updates<br />

Sometimes when you have multiple UpdatePanel controls on a page, you might decide not to update one<br />

of them unless certain conditions are met. In this case, you would configure the UpdateMode property of<br />

the panel to Conditional as shown in the previous section <strong>and</strong> possibly also set the ChildrenAsTriggers<br />

property to false. Then, in your server-side event-h<strong>and</strong>ler code for one of the controls on the page that<br />

causes a partial-page update, you would (conditionally) call the Update() method of the UpdatePanel.<br />

For example:<br />

protected void Button1_Click(object sender, EventArgs e)<br />

{<br />

if (TestSomeCondition())<br />

{<br />

UpdatePanel1.Update();<br />

}<br />

}<br />

UpdatePanel Triggers<br />

You can cause an UpdatePanel control to be updated by a control elsewhere on the web page by adding<br />

triggers to the Triggers property of the control. A trigger is an association between an event of a control<br />

elsewhere on the page <strong>and</strong> the UpdatePanel control. All controls have default events (for example, the<br />

default event of a Button control is Click), so specifying the name of an event is optional. There are two<br />

types of triggers that you can add, represented by the following two classes:<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!