Programming Microsoft ASP.NET 4 - Dino Esposito [442]
You do much of your ASP.NET Web Forms programming using server controls. A server control normally emits HTML markup. In an Ajax scenario, a server control emits markup plus some script code to support Ajax requests. This is not exactly a change of paradigm in the name of Ajax, but it is a good compromise between the classic Web and requested Ajax capabilities.
ASP.NET partial rendering works according to this idea. It provides a new container control—the UpdatePanel control—that you use to surround portions of existing pages, or portions of new pages developed with the usual programming model of ASP.NET. A postback that originates within any of these updatable regions is intercepted by some JavaScript code that the UpdatePanel control has injected in the page. As a result, only the controls in a given region are updated.
The UpdatePanel control, however, requires the use of another server control—the ScriptManager control—which is responsible, among other things, for injecting in the page any required script code.
The ScriptManager Control
The main control in the server infrastructure of ASP.NET for Ajax is the ScriptManager control and its twin, the ScriptManagerProxy control. You will find just one instance of the ScriptManager control in each ASP.NET Ajax page. The ScriptManagerProxy control is used only in master pages scenarios to reference the original script manager from content pages.
The ScriptManager control manages and delivers script resources, thus enabling client scripts to make use of the JavaScript type system extensions and other JavaScript features that we covered earlier in this chapter. The ScriptManager control also enables features such as partial-page rendering and service and page method calls. The following code shows the simplest and most common way to insert the script manager in an ASP.NET page:
The control produces no user interface, works exclusively on the server, and doesn’t add any extra bytes to the page download.
Properties of the ScriptManager Control
The ScriptManager control features a number of properties for you to configure its expected behavior. Table 20-1 details the supported properties.
Table 20-1. Properties of ScriptManager
Property
Description
AjaxFrameworkAssembly
Gets the Ajax framework assembly that components on the page are currently using.
AjaxFrameworkMode
Gets or sets how client scripts of the Microsoft Ajax client library will be included on the client: as local scripts, from ASP.NET assemblies, or nothing.
AllowCustomErrorsRedirect
Indicates whether custom error redirects will occur during an asynchronous postback. The property is set to true by default.
AsyncPostBackErrorMessage
Gets and sets the error message to be sent to the client when an unhandled exception occurs on the server during an asynchronous postback. If this property is not set, the native exception’s message will be used.
AsyncPostBackSourceElementID
Gets the ID of the server control that triggered the asynchronous postback. If there’s no ongoing asynchronous postback, the property is set to the empty string.
AsyncPostBackTimeout
Gets and sets the timeout period in seconds for asynchronous postbacks. A value of zero indicates no timeout. The property is set to 90 by default.
AuthenticationService
Gets an object through which you can set preferences for the client-side authentication service.
ClientNavigateHandler
Indicates the name of the JavaScript method that handles the Sys.Application.navigate event on the client when the user navigates back to a page from the history list.
CompositeScript
Gets a reference to the composite script (if any) for the current page.
EmptyPageUrl
The URL to use if the target Web page is empty during a history navigation.
EnableCdn
Indicates whether client script references are loaded from a content delivery network (CDN) path.
EnableHistory
Indicates whether the page supports history point management.
EnablePageMethods