Adding jQuery to an empty ASP.NET web project using ScriptManager control
Adding jQuery to a web form using the script block has some disadvantages. If the application is upgraded to use the latest version of jQuery, all the web forms with the <script> tag require to be changed. Secondly, switching from the uncompressed version in the development environment to the compressed version in the release environment should be handled manually and is hence error-prone. Using the ASP.NET ScriptManager control helps you overcome this problem. It can also load jQuery directly from CDN instead of using the local copy.
Getting ready
- Create a new ASP.NET Web Application project using the Empty template by following the steps listed in the Adding jQuery to an empty ASP.NET web project using a script block recipe. Name the project
WebApplication2(or any other suitable name). - Follow the steps in the preceding recipe to add the jQuery library (the uncompressed and compressed formats) to the Scripts folder.
- Follow the steps to add a new web form to the project.
How to do it…
Following are the steps to add jQuery to ASP.NET web project using the ScriptManager control:
- Open the web form in the Design mode.
- Launch the Toolbox. This can be done in two ways. From the File menu at the top of the page, go to View | Toolbox. Alternatively, use the shortcut keys, Ctrl + Alt + X.
- Go to Toolbox | AJAX Extensions, and drag and drop the ScriptManager control onto the form:

- Right-click on the project in the Solution Explorer tab, and go to Add | New Item.... From the dialog box, select Global Application Class. This will add the Global.asax file to the project:

Note
The
Global.asaxfile is an optional file that resides in the root directory of the application and responds to events at the application and session levels, such as the starting and ending an application or session. - Open the
Global.asaxfile and include the following namespace at the top of the page:For VB, the code is as follows:
Imports System.Web.UI
For C#, the code is as follows:
using System.Web.UI;
- In the
Application_Startevent in theGlobal.asaxfile, add the following code to create a script that maps to jQuery:For VB, the code is as follows:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ScriptManager.ScriptResourceMapping.AddDefinition("jquery", New ScriptResourceDefinition() With { .Path = "~/Scripts/jquery-2.1.4.min.js", .DebugPath = "~/Scripts/jquery-2.1.4.js", .CdnPath = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js", .CdnDebugPath = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.js", .CdnSupportsSecureConnection = True, .LoadSuccessExpression = "window.jQuery"}) End SubFor C#, the code is as follows:
protected void Application_Start(object sender, EventArgs e) { ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition { Path = "~/Scripts/jquery-2.1.4.min.js", DebugPath = "~/Scripts/jquery-2.1.4.js", CdnPath = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js", CdnDebugPath = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.js", CdnSupportsSecureConnection = true, LoadSuccessExpression = "window.jQuery" }); } - Open the
Default.aspxweb form in the Source mode. Add the followingScriptReferenceto theScriptManagercontrol:<asp:ScriptManager ID="ScriptManager1" runat="server"> <Scripts> <asp:ScriptReference Name="jquery" /> </Scripts> </asp:ScriptManager>Note
When using the
ScriptManagercontrol to add a reference to the jQuery library, the jQuery code should be placed after theScriptManagercontrol, that is, after the jQuery reference has been declared; otherwise, the page will throw an error. It is also important to note that theScriptManagercontrol should reside inside the<form>element. - To retrieve the jQuery files from CDN, set the
EnableCdnproperty of theScriptManagercontrol totrue, as follows:<asp:ScriptManager ID="ScriptManager1" runat="server" EnableCdn="true"> <Scripts> <asp:ScriptReference Name="jquery" /> </Scripts> </asp:ScriptManager>
How it works…
This is how the ScriptManager control works:
- The
ScriptManagercontrol can be used to load JavaScript files, such as the jQuery library. This can be done by adding theScriptReferenceto jQuery in theScriptManagercontrol, as follows:<asp:ScriptReference Name="jquery" />
- However, we require to define this mapping. This can be done in the
Global.asaxfile using aScriptResourceDefinitionobject, which exposes the following properties:Property
Description
PathThis is the release path of the script resource
DebugPathThis is the development/debug path of the script resource
CdnPathThis is the release path of the script resource served from a CDN
CdnDebugPathThis is the development/debug path of the script resource served from a CDN
CdnSupportsSecureConnectionThis indicates whether the HTTPS mode needs to be used to retrieve the resource when the page is accessed using a secure connection
LoadSuccessExpressionThis is the JavaScript expression that detects whether a JavaScript file has been loaded successfully
- The
ScriptResourceDefinitionobject defined inGlobal.asaxis namedjquery. TheScriptManagercontrol uses the same name to load the reference on the web form. - In the development/debug mode, the script is served from
DebugPathwhile in the release mode, it is served fromPath.Tip
Running in development/debug and release modes
To run the application in the development/debug mode, set the
debugattribute of the<compilation>element in theweb.configtotrueas follows:<system.web> <compilation debug="true"/> ….. </system.web>When the
debugattribute is set tofalse, the application will run in the release mode. - If
EnableCdnis set totrue, the script is served from the CDN path, that is, fromCdnDebugPathin the development/debug mode andCdnPathin the release mode. - The
LoadSuccessExpressionproperty renders an inline script to load the library from the local path in the event of a CDN failure. By right-clicking on the web page and viewing the source, note that theScriptManagercontrol adds a fall back mechanism when the CDN is unavailable and files are served locally instead:
See also
The Adding jQuery to an empty ASP.NET web project using a script block recipe