About Me

Professional Practical HumanBeing

Saturday, November 13, 2010

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



No comments:

Post a Comment