Monday, January 21, 2013

The ScriptManager in ASP.NET

In order to use ASP.NET AJAX, you need to place a new web control on your page. This control is the ScriptManager, and it’s the brains of ASP.NET AJAX. Like all ASP.NET AJAX controls, the ScriptManager is placed on a Toolbox tab named AJAX Extensions. When you can drag the ScriptManager onto your page, you’ll end up with this declaration:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
At design time, the ScriptManager appears as a blank gray box. But when you request a page that uses the ScriptManager you won’t see anything, because the ScriptManager doesn’t generate any HTML tags. Instead, the ScriptManager performs a different task—it adds the links to the ASP.NET AJAX JavaScript libraries. It does that by inserting a script block that looks something like this:
<script src="/YourWebSite/ScriptResource.axd?d=RUSU1mI ..."
This script block doesn’t contain any code. Instead, it uses the src attribute to pull the JavaScript code out of a separate file. However, the ScriptManager is a bit craftier than you might expect. Rather than use a separate file to get its JavaScript (which would then need to be deployed along with your application), the src attribute uses a long, strange-looking URL that points to ScriptResource.axd. ScriptResource.axd isn’t an actual file—instead, it’s a resource that tells ASP.NET to find a JavaScript file that’s embedded in one of the compiled .NET 3.5 assemblies. The long query string argument at the end of the URL tells the ScriptResource.axd extension which file to send to the browser.

No comments:
Write comments
Recommended Posts × +