Programming Microsoft ASP.NET 4 - Dino Esposito [445]
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 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 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,
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