Wahlin on .NET
It's All About the Scripts
Simplify your work with JavaScript files and add cross-browser, AJAX-capable scripts into Web pages.
AJAX-enabled Web sites rely heavily upon JavaScript. Without JavaScript, Web pages that leverage AJAX are practically rendered useless. While writing JavaScript code is fairly straightforward, writing and managing cross-browser AJAX scripts can be challenging — this is where the ASP.NET AJAX Extensions really shine. In this column, I'll provide you with an introductory look at the ASP.NET AJAX ScriptManager control, and show how you can use it to simplify your work with JavaScript files and add cross-browser, AJAX-capable scripts into Web pages.
While the XMLHttpRequest object is critical to AJAX success, you must use JavaScript as a necessary partner. After all, without JavaScript, you would not be able to invoke the XMLHttpRequest object to make asynchronous requests from a browser to a server. The ASP.NET AJAX Framework relies on several JavaScript files and internal functions and methods to shield you from the complexities of using the XMLHttpRequest object and other browser objects and technologies.
JavaScript files used by the ASP.NET AJAX Framework are built into the System.Web.Extensions.dll assembly. This DLL file is placed in the GAC when you install the ASP.NET AJAX Extensions. You can use a tool such as Reflector to view the script resources embedded in the assembly; you can also find the scripts here:
C:\Program Files\Microsoft ASP.NET \ASP.NET 2.0 AJAX Extensionsv1.0.61025\MicrosoftAjaxLibrary \System.Web.Extensions\1.0.61025.0
Debug and release versions of the scripts are available at this location. The debug scripts are nicely formatted for easy reading and debugging, and the whitespaces are stripped out of the release scripts to minimize their size.
The ScriptManager control loads the scripts from the ASP.NET AJAX library. These scripts are required to support other AJAX controls and tasks performed in an AJAX-enabled Web page. The ScriptManager control also performs several other tasks such as loading debugged versions of a script, loading custom scripts and creating Web service client-side proxies.
You can define a basic ScriptManager control in an ASP.NET AJAX Web page using the following syntax:
<asp:ScriptManager id="sm"
runat="Server" />
The ScriptManager control relies on the ScriptResourceHandler HttpHandler (see my previous column on ASP.NET AJAX configuration) to access the scripts embedded in the System.Web.Extensions.dll assembly. The AJAX features needed by controls in a Web page determine which scripts are dynamically loaded from the assembly. Listing 1 shows an example of standard script output that's added to a Web page after the ScriptManager control executes.
The first script reference in Listing 1 loads a script file named MicrosoftAjax.js that contains the core functionality for ASP.NET AJAX Web pages. The second script reference loads a script file named MicrosoftAjaxWebForms.js that contains functionality for handling client-side events with a JavaScript class named PageRequestManager. The browser caches these scripts by default, which enhances the overall load time of pages that use the scripts on a frequent basis.
The ScriptManager control does more than load ASP.NET AJAX Framework scripts. You can use its Scripts property and associated ScriptReference control to load custom scripts as well. Listing 2 shows an example of how you can use the ScriptManager control to load a custom script named MyScript.js. Custom scripts are rendered during the ScriptManager's PreRender event and are immediately added after the first two script references in Listing 1.
It's important to note that custom scripts using the ScriptManager must notify the client-side ASP.NET AJAX Framework when they have finished loading. You can make sure this process occurs by adding this line of code at the bottom of your custom script file:
if (typeof(Sys) !== 'undefined')
Sys.Application.notifyScriptLoaded();
In cases where you want to load custom scripts without using the ScriptManager control, you can use the standard HTML <script> tag. However, you'll want to ensure that your scripts are loaded at the appropriate time during your Web page's client-side life-cycle, especially if your scripts rely on functionality defined in ASP.NET AJAX library scripts.
The ScriptManager control loads core ASP.NET AJAX Framework scripts that add AJAX functionality into your Web pages. You must add the a ScriptManager control to a Web page anytime you want to use ASP.NET AJAX with new or existing ASP.NET Web pages. In future columns, I will provide additional details on how you can use the ScriptManager control to work with debug and release scripts, as well as generate client-side Web service proxies.
About the Author
Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.