-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
ASP.NET jQuery Cookbook (Second Edition) - Second Edition
By :
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.
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.
WebApplicationWithMaster (or any other suitable name).Default.Master, and click on Add:
Default.aspx, and click on Add:

To incorporate jQuery in an ASP.NET Master Page, follow these steps:
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:

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.
ContentPlaceHolder (with ID = "ContentPlaceHolder1") in the Default.aspx web form.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:

The Adding jQuery to an empty ASP.NET web project using the ScriptManager control recipe
Change the font size
Change margin width
Change background colour