Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ASP.NET jQuery Cookbook (Second Edition)

You're reading from   ASP.NET jQuery Cookbook (Second Edition) Over 60 recipes for writing client script in ASP.NET 4.6 applications using jQuery

Arrow left icon
Product type Paperback
Published in Feb 2016
Publisher
ISBN-13 9781782173113
Length 478 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Sonal Merchant Sonal Merchant
Author Profile Icon Sonal Merchant
Sonal Merchant
Sonal Aneel Allana Sonal Aneel Allana
Author Profile Icon Sonal Aneel Allana
Sonal Aneel Allana
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Getting Started with jQuery in ASP.NET FREE CHAPTER 2. Using jQuery Selectors with ASP.NET Controls 3. Event Handling Using jQuery 4. DOM Traversal and Manipulation in ASP.NET 5. Visual Effects in ASP.NET Sites 6. Working with Graphics in ASP.NET Sites 7. Ajax Using jQuery 8. Creating and Using jQuery Plugins Index

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

  1. 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).
  2. Follow the steps in the preceding recipe to add the jQuery library (the uncompressed and compressed formats) to the Scripts folder.
  3. 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:

  1. Open the web form in the Design mode.
  2. 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.
  3. Go to Toolbox | AJAX Extensions, and drag and drop the ScriptManager control onto the form:
    How to do it…
  4. 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:
    How to do it…

    Note

    The Global.asax file 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.

  5. Open the Global.asax file 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;
  6. In the Application_Start event in the Global.asax file, 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 Sub

    For 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"
        });
    }
  7. Open the Default.aspx web form in the Source mode. Add the following ScriptReference to the ScriptManager control:
    <asp:ScriptManager ID="ScriptManager1" runat="server">
         <Scripts>
              <asp:ScriptReference Name="jquery"  />
         </Scripts>
    </asp:ScriptManager>

    Note

    When using the ScriptManager control to add a reference to the jQuery library, the jQuery code should be placed after the ScriptManager control, that is, after the jQuery reference has been declared; otherwise, the page will throw an error. It is also important to note that the ScriptManager control should reside inside the <form> element.

  8. To retrieve the jQuery files from CDN, set the EnableCdn property of the ScriptManager control to true, 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:

  1. The ScriptManager control can be used to load JavaScript files, such as the jQuery library. This can be done by adding the ScriptReference to jQuery in the ScriptManager control, as follows:
    <asp:ScriptReference Name="jquery"  />
  2. However, we require to define this mapping. This can be done in the Global.asax file using a ScriptResourceDefinition object, which exposes the following properties:

    Property

    Description

    Path

    This is the release path of the script resource

    DebugPath

    This is the development/debug path of the script resource

    CdnPath

    This is the release path of the script resource served from a CDN

    CdnDebugPath

    This is the development/debug path of the script resource served from a CDN

    CdnSupportsSecureConnection

    This indicates whether the HTTPS mode needs to be used to retrieve the resource when the page is accessed using a secure connection

    LoadSuccessExpression

    This is the JavaScript expression that detects whether a JavaScript file has been loaded successfully

  3. The ScriptResourceDefinition object defined in Global.asax is named jquery. The ScriptManager control uses the same name to load the reference on the web form.
  4. In the development/debug mode, the script is served from DebugPath while in the release mode, it is served from Path.

    Tip

    Running in development/debug and release modes

    To run the application in the development/debug mode, set the debug attribute of the <compilation> element in the web.config to true as follows:

    <system.web>
        <compilation debug="true"/>
        …..
    </system.web>  

    When the debug attribute is set to false, the application will run in the release mode.

  5. If EnableCdn is set to true, the script is served from the CDN path, that is, from CdnDebugPath in the development/debug mode and CdnPath in the release mode.
  6. The LoadSuccessExpression property 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 the ScriptManager control adds a fall back mechanism when the CDN is unavailable and files are served locally instead:
    How it works…

See also

The Adding jQuery to an empty ASP.NET web project using a script block recipe

You have been reading a chapter from
ASP.NET jQuery Cookbook (Second Edition) - Second Edition
Published in: Feb 2016
Publisher:
ISBN-13: 9781782173113
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image