15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

The scriptManager Control<br />

As mentioned earlier in the chapter, the ScriptManager control must be included on all pages that use<br />

partial - page postbacks <strong>and</strong> several other aspects of ASP.<strong>NET</strong> AJAX functionality.<br />

A great way to ensure that all the pages in your web application contain the<br />

ScriptManager control is to add this control to the master page (or master pages) that<br />

your application uses.<br />

As well as enabling ASP.<strong>NET</strong> AJAX functionality, you can also use properties to confi gure this control. The<br />

simplest of these properties is EnablePartialRendering , which is true by default. If you set this property<br />

to false , you will disable all asynchronous postback processing, such as that provided by UpdatePanel<br />

controls. This can be useful, for example, if you want to compare your AJAX - enabled web site with a<br />

traditional web site; perhaps if you are giving a demonstration to a manager.<br />

You can use the ScriptManager control for several reasons, such as in the following common situations:<br />

➤<br />

➤<br />

➤<br />

➤<br />

To determine whether server - side code is being called as a result of a partial - page postback<br />

To add references to additional client - side JavaScript fi les<br />

To reference Web services<br />

To return error messages to the client<br />

These confi guration options are covered in the following sections.<br />

Detect Partial - Page Postbacks<br />

The ScriptManager control includes a Boolean property called IsInAsyncPostBack . You can use<br />

this property in server - side code to detect whether a partial - page postback is in progress. Note that the<br />

ScriptManager for a page may actually be on a master page. Rather than accessing this control through<br />

the master page, you can obtain a reference to the current ScriptManager instance by using the static<br />

GetCurrent() method, for example:<br />

ScriptManager scriptManager = ScriptManager.GetCurrent(this);<br />

if (scriptManager != null & & scriptManager.IsInAsyncPostBack)<br />

{<br />

// Code to execute for partial - page postbacks.<br />

}<br />

You must pass a reference to a Page control to the GetCurrent() method. For example, if you use this<br />

method in a Page_Load() event h<strong>and</strong>ler for an ASP.<strong>NET</strong> web page, you can use this as your Page<br />

reference. Also, remember to check for a null reference to avoid exceptions.<br />

Client - Side JavaScript References<br />

Rather than adding code to the HTML page header, or in elements on the page, you can use the<br />

Scripts property of the ScriptManager class. This centralizes your script references <strong>and</strong> makes it easier to<br />

maintain them. You can do this declaratively by adding a child element to the <br />

control element, <strong>and</strong> then adding child control elements to . You use<br />

the Path property of a ScriptReference control to reference a custom script.<br />

The following sample shows how to add references to a custom script fi le called MyScript.js in the root<br />

folder of the web application:<br />

< asp:ScriptManager runat="server" ID="ScriptManager1" ><br />

< Scripts ><br />

< asp:ScriptReference Path="~/MyScript.js" / ><br />

< /Scripts ><br />

< /asp:ScriptManager ><br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!