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 programmatically to a web form

In addition to adding jQuery to web forms using the script block and the ScriptManager control, the code-behind file can also emit the required script code. This recipe will demonstrate how this can be done.

Getting ready

  1. Create an ASP.NET Web Application project by navigating to File | New | Project | ASP.NET Web Application. Select the Empty template. Name the project WebApplicationWithPageLoad (or any other suitable name).
  2. Add a new Web Form to the project and name it Default.aspx.
  3. Add the jQuery library files to the Scripts folder.
  4. From the Solution Explorer tab, navigate to Default.aspx.vb (VB) or Default.aspx.cs (C#), which is the code-behind file for the web form. Open this file.

How to do it…

In the Page_Load event handler of Default.aspx.vb, use the RegisterClientScriptInclude method to generate a script block on the page, as follows:

For VB, the code is as follows:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   Page.ClientScript.RegisterClientScriptInclude("jquery",   Page.ResolveUrl("~/Scripts/jquery-2.1.4.js"))
End Sub

For C#, the code is as follows:

protected void Page_Load(object sender, EventArgs e)
{
   Page.ClientScript.RegisterClientScriptInclude("jquery",   Page.ResolveUrl("~/Scripts/jquery-2.1.4.js"));
}

How it works…

The RegisterClientScriptInclude method requires two parameters: the key and URL. It adds the script block with the path to the jQuery library in the <form> element, as shown in the following screenshot. The Page.ResolveUrl method is used to return a URL relative to the site root:

How it works…

Since the jQuery library is added to the <form> element, all the jQuery code should be written in the <form> element instead of the <head> element, preferably toward the end of the page before closing the <form> element.

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