About Me

Professional Practical HumanBeing

Saturday, November 13, 2010

UpdatePanel

The UpdatePanel control, is responsible for partial page rendering of a web page. Meaning only part of a page is posted back to the server instead of the entire page. This helps to build rich User Interfaces, provides improved performance and quicker response time.

Properties of the UpdatePanel
The content of the UpdatePanel described in the code listing is declared within the ContentTemplate tag. The content of the ContentTemplate tag is posted back to the server asynchronously. Create the ContentTemplate's content declaratively or programmatically.

By default the UpdateMode property of UpdatePanel is set to ‘Always’ meaning the contents of the UpdatePanel will be rendered whenever the page is posted back to server. Conversely setting the UpdateMode property to ‘Conditional’ means the content of the UpdatePanel will be rendered in the following circumstances:

  • When the Update method is explicitly called from the UpdatePanel control
  • When a control defined as a trigger within the Triggers tag in the UpdatePanel and is responsible for postback. The control triggers an update of the contents within the panel explicitly and the control can be present either inside or outside of the UpdatePanel control.
  • When a child control of an UpdatePanel control is responsible for postback and the ChildrenAsTriggers property is set to true
  • When the UpdatePanel control is nested inside another UpdatePanel control and the parent UpdatePanel control is updated

The following code shows the Update method of the UpdatePanel being called provided the condition within the if block is satisfied.

Code Listing

protected void Button1_Click(object sender, EventArgs e)
{
if (condition.....)
{
UpdatePanel1.Update();
}
}

Nested Updat

ePanel Controls
The UpdatePanel controls can be nested. If the parent UpdatePanel is refreshed, all the nested UpdatePanels are refreshed also.

The element
The above code sends not only the label’s code when asynchronous post back occurs, but also the button’s code and the entire section of the UpdatePanel back to the client. However only the part which needs updating can be sent back to the server - meaning only the label part. To do this,

Trigger elements need to be added into the UpdatePanel control as shown in the Code Listing below:



The Trigger can be generated by any control in the
form. It can be of two types:
AsynchronousPostBackTrigger and PostBackTrigger. In this example AsynchronousPostBackTrigger is used. The PostBackTrigger will cause a full page post back whereas the AsynchronousPostBackTrigger will cause only a Asynchronous PostBack.






Source :-

ScriptManager,ScriptManagerProxy

The ScriptManager control manages client script for AJAX-enabled ASP.NET Web pages. By default, theScriptManager control registers the script for the Microsoft Ajax Library with the page. This enables client script to use the type system extensions and to support features such as partial-page rendering and Web-service calls.

Scenarios:-

You must use a ScriptManager control on a page to enable the following Microsoft Ajax features of ASP.NET:

  • Client-script functionality of the Microsoft Ajax Library, and any custom script that you want to send to the browser.

  • Partial-page rendering, which enables regions on the page to be independently refreshed without a postback. The ASP.NET UpdatePanel, UpdateProgress, and Timer controls require a ScriptManagercontrol in order to support partial-page rendering.

  • JavaScript proxy classes for Web services, which enable you to use client script to access Web services and specially marked methods in ASP.NET pages. It does this by exposing the Web services and page methods as strongly typed objects.

  • JavaScript classes to access ASP.NET authentication, profile, and roles application services.

Ajax and ScriptManager :-

Many developers, when first using ASP.NET AJAX, start with the UpdatePanel control. If a ScriptManager control is included on the page and the UpdatePanel contains any controls, the controls within UpdatePanel can be asynchronously updated through the facilities of AJAX.

If someone clicks the Button in this setup, the Button control will raise a postback event that will be caught by the UpdatePanel control. The UpdatePanel then resubmits the postback event as a partial postback and its content will be asynchronously updated (without the browser fully reloading the page).

However, there are many interesting scenarios where your expectations will fail, leaving you with a mess on your hands.

However, there are many interesting scenarios where your expectations will fail, leaving you with a mess on your hands. For example, there is a new and fascinating way to break script references using the UpdatePanel control and AJAX. In days of old you would have added script to the page as a result of a full postback such as this:
protected void Button1_Click(object sender, EventArgs e) {     Page.ClientScript.RegisterStartupScript(         
this.GetType(),"myscript","alert('hello world!');"); }

But this new method can break under ASP.NET AJAX. Why? Because ClientScriptManager is not a control that understands partial postbacks and partial rendering, and so script code registered with it will not be included in the page response for a partial postback.
Instead, if you wanted the button to introduce client script into the page output, you would leverage the ScriptManager:

protected void Button1_Click(object sender, EventArgs e) {     ScriptManager.RegisterStartupScript(         this,this.GetType(),"myscript","alert('hello world!');",true); } 
The result is the same, but the method is slightly different. In fact, the methods of the ClientScriptManager control are now included in the ScriptManager control as static methods (RegisterStartupScript, for example). Whenever you are utilizing ASP.NET AJAX and want to work with script through partial postbacks, you must now use methods exposed by the ScriptManager control instead.

you want to click the LinkButton and make the UpdatePanel behave as if the LinkButton were inside it. To do this, use a method on the ScriptManager control called RegisterAsyncPostBackControl:

igure 5 Using Link Button Asynchronously
protected void Page_Load(object sender, EventArgs e) {     // register the LinkButton1 control as one that can      // cause partial postbacks.      ScriptManager1.RegisterAsyncPostBackControl(LinkButton1); } 
Clicking the LinkButton will now cause the UpdatePanel to refresh asynchronously, just as if the LinkButton control were actually in the UpdatePanel.

As an example of inverse behavior, you could also make an element inside an UpdatePanel cause the whole page to refresh—a full postback. To do so, you just use a different method on the ScriptManager called RegisterPostBackControl:
protected void Page_Load(object sender, EventArgs e) {     // Button1 will cause a full post-back no matter     // where it is on the page.       ScriptManager1.RegisterPostBackControl(Button1); }

Let's go even further now. You have seen how to tweak the controls that trigger the UpdatePanel control's partial postback event using ScriptManager, but what if you wanted to update a control somewhere on the page—totally outside of the UpdatePanel—when the UpdatePanel refreshes? This is also possible through the ScriptManager control.
ScriptManager's RegisterDataItem method makes it easy to refresh controls or data outside an UpdatePanel. RegisterDataItem allows extra data of your choosing to be sent down to the client when an UpdatePanel control posts back; the data can be consumed by script you write on the client.

For example, say you have a situation like the controls in Figure 6. In this example, I want to update the Label with whatever value has been clicked in the Calendar control. This may seem simple, but the Calendar control is within the UpdatePanel while the Label is not. How can I get the UpdatePanel to refresh the Label? The answer is simple: if I use RegisterDataItem from my server-side code, I can get extra data sent to my client-side code. The client can consume the data sent from RegisterDataItem and refresh the Label with

protected void Calendar1_SelectionChanged(object sender, EventArgs e) {     ScriptManager1.RegisterDataItem(         Label1, Calendar1.SelectedDate.ToShortDateString());  }


ScriptManager and ScriptManagerProxy :-

The ScriptManager is the most important control for an Ajax enabled web page. It provides Client side scripting and access to web service using javascript proxy classes. Using the UpdatePanel control provides partial page rendering capabilities. There can be only one ScriptManager control in an Ajax enabled webpage. If more than one ScriptManager control is added in the web page, the following exception will occur at runtime: “Exception Details: System.InvalidOperationException: Only one instance of a ScriptManager can be added to the page.

However to modify the ScriptManager control of the master page, then have a scriptManagerproxy control in the content page. Note that to use ScriptManagerProxy control in the content page, there should be a ScriptManager control in master page.


Source:-

http://msdn.microsoft.com/en-us/magazine/cc163354.aspx (a Very Good Link)

http://msdn.microsoft.com/en-us/library/bb398863.aspx