Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [451]

By Root 5755 0
others, you change the value of the UpdateMode property. The default value is Always, meaning that the panel’s content is updated on every postback that originates from anywhere in the page, from inside and outside the updatable region.

By changing the value of the UpdateMode property to Conditional, you instruct the updatable panel to update its content only if it is explicitly ordered to refresh. This includes calling the Update method, intercepting a postback from a child control, or handling any of the events declared as triggers.

Normally, any control defined inside of an UpdatePanel control acts as an implicit trigger for the panel. You can stop all child controls from being triggers by setting the value of ChildrenAsTriggers to false. In this case, a button inside an updatable panel, if clicked, originates a regular full postback.

What if you want only a few controls within an UpdatePanel to act as triggers? You can define them as triggers of a particular UpdatePanel, or you can use the RegisterAsyncPostBackControl method on the ScriptManager class.

The RegisterAsyncPostBackControl method enables you to register controls to perform an asynchronous postback instead of a synchronous postback, which would update the entire page. Here is an example of the RegisterAsyncPostBackControl method:

protected void Page_Load(Object sender, EventArgs e)

{

ScriptManager1.RegisterAsyncPostBackControl(Button1);

}

The control object you pass as an argument will be a control not included in any updatable panels and not listed as a trigger. The effects of the postback that originates from the control differ with regard to the number of UpdatePanel controls in the page. If there’s only one UpdatePanel in the page, the script manager can easily figure out which one to update. The following code shows a page whose overall behavior might change if one or two UpdatePanel controls are used:

protected void Button1_Click(Object sender, EventArgs e)

{

// If there's only one UpdatePanel in the page, and it includes this Label control,

// the panel is refreshed automatically.

Label1.Text = "Last update at: " + DateTime.Now.ToLongTimeString();

// This Label control, not included in any UpdatePanel, doesn't have its UI

// refreshed. Its state, though, is correctly updated.

Label2.Text = "Last update at: " + DateTime.Now.ToLongTimeString();

}

When multiple panels exist, to trigger the update you have to explicitly invoke the Update method on the panel you want to refresh:

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text = "Last update at: " + DateTime.Now.ToLongTimeString();

UpdatePanel1.Update();

}

All controls located inside of an UpdatePanel control are automatically passed as an argument to the RegisterAsyncPostBackControl method when ChildrenAsTriggers is true.

Note

A postback that originates from within an UpdatePanel control is often referred to as an asynchronous postback or an AJAX postback. Generally, these expressions are used to reference a postback conducted via a script taking advantage of XMLHttpRequest.

Programmatic Updates


I’ve already mentioned the Update method quite a few times. It’s time to learn more about it, starting with its signature:

public void Update()

The method doesn’t take any special action itself, but is limited to requiring that the child controls defined in the content template of the UpdatePanel control be refreshed. By using the Update method, you can programmatically control when the page region is updated in response to a standard postback event or perhaps during the initialization of the page.

An invalid operation exception can be thrown from within the Update method in a couple of well-known situations. One situation is if you call the method when the UpdateMode property is set to Always. The exception is thrown in this case because a method invocation prefigures a conditional update—you do it when you need it—which is just the opposite of what the Always value of the UpdateMode property indicates. The other situation in which the exception

Return Main Page Previous Page Next Page

®Online Book Reader