Online Book Reader

Home Category

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

By Root 5709 0
when an exception goes unhandled on the server during an asynchronous postback.

Navigate

Occurs when the user clicks the browser’s Back or Forward button.

ResolveCompositeScriptReference

Occurs when the ScriptManager control is going to resolve a composite script reference.

ResolveScriptReference

Occurs when the ScriptManager control is going to resolve a script reference.

These events are much more than mere notifications of something that has happened on the server. Both give you good chances to intervene effectively in the course of the application. For example, by handling the ResolveScriptReference event, you can change the location from where the script is going to be downloaded on the client:

protected void ResolveScript(object sender, ScriptReferenceEventArgs e)

{

// Check Path or Name on the e.Script object based on what you've put in Scripts.

// Next, you specify the real file to load

if (String.Equals(e.Script.Path, "personal.js", StringComparison.OrdinalIgnoreCase))

e.Script.Path = "person.js";

}

By handling the AsyncPostBackError event, you can edit the error message being returned to the client during a partial rendering operation. Here’s an example:

protected void AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)

{

ScriptManager sm = sender as ScriptManager;

if (Request.UserHostAddress == "127.0.0.1")

{

sm.AsyncPostBackErrorMessage = String.Format(

"An error occurred.
{0}",

e.Exception.Message);

}

else

{

sm.AsyncPostBackErrorMessage = String.Format(

"An error occurred.
{0}",

"Please contact your Web master.");

}

}

What if you want to redirect the user to an error page instead?

In this case, you configure the page to use the traditional error-handling mechanism for ASP.NET pages. You configure the section in the web.config file and indicate HTML error pages to reach in case of specific errors. This behavior can be disabled by setting to false the value of the AllowCustomErrorRedirects property of the ScriptManager object.

Note

When an exception is thrown during a partial rendering operation, the HTTP request returns a regular HTTP 200 status code, but instead of including the updated markup, it includes a message in which a flag indicates the success or failure of the operation. In addition, the message includes the full description of the error or the updated markup. In case of error, the client-side default error handler throws a JavaScript exception passing the error message as an argument.

The ScriptManagerProxy Control


Only one instance of the ScriptManager control can be added to an ASP.NET AJAX page. However, there are two ways in which you can do this. You can add it directly on the page using the tag or indirectly by importing a component that already contains a script manager. Typically, you can accomplish the second alternative by importing a user control, creating a content page for a master page, or authoring a nested master page.

What if a content page needs to add a new script reference to the manager? In this case, you need a reference to the script manager. Although it’s defined in the master page (or in a user control), the script manager might not be publicly exposed to the content page. You can use the static method GetCurrent on the class ScriptManager to get the right instance:

// Retrieve the instance of the ScriptManager active on the page

var manager = ScriptManager.GetCurrent(this.Page);

The ScriptManagerProxy class saves you from this sort of coding. In general, in cases where you need features of the ScriptManager control but lack a direct reference to it, you can instead include a ScriptManagerProxy control in the content page.

You can’t have two script managers in the context of the same page; however, you can have a script manager and a proxy to retrieve it. The ScriptManagerProxy control enables you to add scripts and services to nested components, and it enables partial page updates in user controls and nested master pages. When you use the proxy,

®Online Book Reader