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 ASP.NET Master Page

Master Pages are used to achieve a uniform look and feel in the website. They maintain a consistent layout across all the content pages. Including jQuery in the Master Page ensures that all the content pages using that Master Page will also have the library included by default. This recipe will demonstrate how this can be done.

Note

A Master Page is an ASP.NET file with the .Master extension. It has a @Master directive at the top of the layout instead of the @Page directive in an ordinary .aspx page.

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 WebApplicationWithMaster (or any other suitable name).
  2. Follow the steps in the previous recipe to add the jQuery library (the uncompressed and compressed formats) to the Scripts folder.
  3. In the Solution Explorer tab, right-click on the project, and go to Add | New Item.... This will launch a dialog box, as shown in the following screenshot. From the dialog box, select Web Forms Master Page. Name the Master Page Default.Master, and click on Add:
    Getting ready
  4. To add a web form—that is, a content page—to the project, right-click on the project in the Solution Explorer tab again, and navigate to Add | New Item.... From the dialog box, this time select Web Form with Master Page, as shown in the following screenshot. Name the web form Default.aspx, and click on Add:
    Getting ready
  5. This will launch a dialog box so that you can select the Master Page. From the dialog box, as shown in the following screenshot, select the Master Page to be associated with the content page, and click on OK:
    Getting ready

How to do it…

To incorporate jQuery in an ASP.NET Master Page, follow these steps:

  1. Open the Default.Master Master Page in the Source mode, and add a reference to the jQuery library using either the <script> block (refer to the Adding jQuery to an empty ASP.NET web project using a script block recipe) or the ScriptManager control (refer to the Adding jQuery to an empty ASP.NET web project using the ScriptManager control recipe), as shown in the following screenshot:
    How to do it…
    How to do it…

    Note

    When using the <script> block, the jQuery reference should preferably be placed in the <head> element.

    When using the ScriptManager control, the control should preferably be placed in the <form> element before the ContentPlaceHolder in which the jQuery code will be added later to the content pages. The Global.asax file should also be updated in order to add the required ScriptResourceDefinition , as described in the Adding jQuery to an empty ASP.NET web project using the ScriptManager control recipe.

  2. The required jQuery code can now be added to the ContentPlaceHolder (with ID = "ContentPlaceHolder1") in the Default.aspx web form.

How it works…

On running the application, when the Default.aspx content page is loaded, the HTML markup from the Master page adds the reference to the jQuery library. This makes the content page jQuery-ready so that any jQuery code can be executed.

To check whether the jQuery reference has been added to the page, run the project and launch Default.aspx in the browser. Right-click on the page in the browser window and select View Source. The jQuery reference will be seen on the page, as shown in the following screenshot:

How it works…

See also

The Adding jQuery to an empty ASP.NET web project using the ScriptManager control 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