ASP.NET jQuery Cookbook - Second Edition - Sonal Aneel Allana - E-Book

ASP.NET jQuery Cookbook - Second Edition E-Book

Sonal Aneel Allana

0,0
47,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Over 60 recipes for writing client script in ASP.NET 4.6 applications using jQuery

About This Book

  • This is a step-by-step guide to solve common problems in ASP.NET applications
  • Easy-to-follow recipes to write client scripts effortlessly and quickly
  • A hands-on approach that will show you how to interface jQuery with ASP.NET websites and MVC

Who This Book Is For

This book is for ASP.NET developers who want to use jQuery to write client scripts for cross-browser compatibility. The book assumes no prior knowledge of ASP.NET or jQuery, and every recipe is self-contained and explained in an easy-to-follow manner. Familiarity with Visual Studio and MS SQL Server is preferred, but not compulsory.

What You Will Learn

  • Download and include jQuery in ASP.NET websites and MVC
  • Use jQuery selectors with ASP.NET server controls
  • Get to know about event handling and DOM traversal
  • Deploy jQuery for visual effects and animations
  • Develop AJAX-enabled ASP.NET applications
  • Create your own plugins
  • Solve common problems using less code and cut down your development time

In Detail

jQuery is a lightweight JavaScript library that has changed the landscape of client scripting in web applications. Developed by John Resig in 2006, it has taken the web by storm because of its cross-browser compatibility and the ability to get more done with less code. It has gained popularity with ASP.NET developers and is now distributed with Visual Studio and the NuGet package manager.

ASP.NET jQuery Cookbook explores the wide range of utilities that the jQuery library provides. It teaches you the nitty-gritty of plugging in these features in ASP.NET web applications. It covers every aspect of interfacing the library, right from downloading and including jQuery on web pages to selecting controls, handling events, and creating animations. This book also walks you through DOM traversal and manipulation in ASP.NET and then through visual effects and graphics in ASP.NET sites. It explores advanced features such as posting AJAX requests and writing plugins. It will provide you with all the information you need to use this library confidently with ASP.NET.

Style and approach

This book takes a hands-on, pragmatic approach to solving common problems in ASP.NET applications using jQuery. Every recipe has detailed steps with screen captures and code snippets. The necessary theoretical concepts are introduced as and when needed.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 376

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

ASP.NET jQuery Cookbook Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started with jQuery in ASP.NET
Introduction
Downloading jQuery from jQuery.com
Getting ready
How to do it…
See also…
Understanding CDN for jQuery
How to do it…
Using CDNs for new releases
How it works…
See also
Using NuGet Package Manager to download jQuery
Getting ready
How to do it…
How it works…
See also
Adding jQuery to an empty ASP.NET web project using a script block
Getting ready
How to do it…
See also
Adding jQuery to an empty ASP.NET web project using ScriptManager control
Getting ready
How to do it…
How it works…
See also
Adding jQuery to an ASP.NET Master Page
Getting ready
How to do it…
How it works…
See also
Adding jQuery programmatically to a web form
Getting ready
How to do it…
How it works…
See also
Understanding jQuery reference in the default web application template
How to do it...
How it works…
See also
Hello World in a web project using jQuery
Getting ready
How to do it…
How it works…
See also
Bundling jQuery in ASP.NET MVC
Getting ready
How to do it…
How it works…
See also
Using CDN to load jQuery in MVC
Getting ready
How to do it…
How it works…
See also
Hello World in ASP.NET MVC using jQuery
Getting ready
How to do it…
How it works…
See also
Debugging jQuery code in Visual Studio
Getting ready
How to do it…
See also
2. Using jQuery Selectors with ASP.NET Controls
Introduction
Selecting a control using ID and displaying its value
Getting ready
How to do it…
How it works…
See also
Selecting a control using the CSS class
Getting ready
How to do it…
How it works…
See also
Selecting a control using HTML tag
Getting ready
How to do it…
How it works…
See also
Selecting a control by its attribute
Getting ready
How to do it…
How it works…
See also
Selecting an element by its position in the DOM
Getting ready
How to do it…
How it works…
See also
Enabling/disabling controls on a web form
Getting ready
How to do it…
How it works…
See also
Using selectors in MVC applications
Getting ready
How to do it…
How it works…
See also
3. Event Handling Using jQuery
Introduction
jQuery event binders
Responding to mouse events
Getting ready
How to do it…
How it works…
See also
Responding to keyboard events
Getting ready
How to do it…
How it works…
See also
Responding to form events
Getting ready
How to do it…
How it works…
See also
Using event delegation to attach events to future controls
Getting ready
How to do it…
How it works…
See also
Running an event only once
Getting ready
How to do it…
How it works…
See also
Triggering an event programmatically
Getting ready
How to do it…
How it works…
See also
Passing data with events and using event namespacing
Getting ready
How to do it…
How it works…
See also
Detaching events
Getting ready
How to do it…
How it works…
See also
4. DOM Traversal and Manipulation in ASP.NET
Introduction
Adding/removing DOM elements
Getting ready
How to do it…
How it works…
See also
Accessing parent and child controls
Getting ready
How to do it…
How it works…
See also
Accessing sibling controls
Getting ready
How to do it…
How it works…
There's more…
See also
Refining selection using a filter
Getting ready
How to do it…
How it works…
There's more…
See also
Adding items to controls at runtime
Getting ready
How to do it…
How it works…
See also
5. Visual Effects in ASP.NET Sites
Introduction
Animating the Menu control
Getting ready
How to do it…
How it works…
See also
Animating a Label control to create a digital clock
Getting ready
How to do it…
How it works…
See also
Animating the alt text of the AdRotator control
Getting ready
How to do it…
How it works…
There's more…
See also
Animating images in the TreeView control
Getting ready
How to do it…
How it works…
There's more…
See also
Creating scrolling text in a Panel control
Getting ready
How to do it…
How it works…
See also
Creating a vertical accordion menu using Panel controls
Getting ready
How to do it…
How it works…
See also
Showing/hiding the GridView control with the explode effect
Getting ready
How to do it…
How it works…
See also
6. Working with Graphics in ASP.NET Sites
Introduction
Creating a spotlight effect on images
Getting ready
How to do it…
How it works…
See also
Zooming images on mouseover
Getting ready
How to do it…
How it works…
See also
Creating an image scroller
Getting ready
How to do it…
How it works…
See also
Building a photo gallery using z-index property
Getting ready
How to do it…
How it works…
See also
Building a photo gallery using ImageMap control
Getting ready
How to do it…
How it works…
See also
Using images to create effects in the Menu control
Getting ready
How to do it…
How it works…
See also
Creating a 5 star rating control
Getting ready
How to do it…
How it works…
There's more…
See also
Previewing image uploads in MVC
Getting ready
How to do it…
How it works…
See also
7. Ajax Using jQuery
Introduction
Setting up Ajax with ASP.NET using jQuery
Getting ready
How to do it…
How it works…
There's more…
See also
Consuming page methods
Getting ready
How to do it…
How it works…
See also
Consuming Web services
Getting ready
How to do it…
How it works…
See also
Consuming WCF services
Getting ready
How to do it…
How it works…
See also
Retrieving data from a Web API
Getting ready
How to do it…
How it works…
See also
Making Ajax calls to a controller action
Getting ready
How to do it…
How it works…
See also
Making Ajax calls to a HTTP handler
Getting ready
How to do it…
How it works…
See also
8. Creating and Using jQuery Plugins
Introduction
Creating and using a simple plugin
Getting ready
How to do it…
How it works…
See also
Using the $ alias in the plugin
Getting ready
How to do it…
How it works…
There's more
See also
Calling methods on DOM elements
Getting ready
How to do it…
How it works…
There's more…
See also
Providing default values
Getting ready
How to do it…
How it works…
There's more…
See also
Providing method chaining
Getting ready
How to do it…
How it works…
See also
Adding actions to plugins
Getting ready
How to do it…
How it works…
See also
Using the form validation plugin
Getting ready
How to do it…
How it works…
There's more…
See also
Downloading plugins using the NPM
Getting ready
How to do it…
How it works…
See also
Index

ASP.NET jQuery Cookbook Second Edition

ASP.NET jQuery Cookbook Second Edition

Copyright © 2016 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: April 2011

Second edition: February 2016

Production reference: 1220216

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78217-311-3

www.packtpub.com

Credits

Author

Sonal Aneel Allana

Reviewer

Ayad Boudiab

Commissioning Editor

Dipika Gaonkar

Acquisition Editor

Divya Poojari

Content Development Editor

Priyanka Mehta

Technical Editors

Dhiraj Chandanshive

Devesh Chugh

Copy Editor

Rashmi Sawant

Project Coordinator

Izzat Contractor

Proofreader

Safis Editing

Indexer

Priya Sane

Graphics

Jason Monteiro

Production Coordinator

Shantanu N. Zagade

Cover Work

Shantanu N. Zagade

About the Author

Sonal Aneel Allana works as a sessional lecturer at the Singapore campus of the University of Newcastle and the University of Hertfordshire. Her teaching areas include degree level courses in e-learning, intelligent systems, robotics, operating systems, and programming in C/C++, .NET, Java, and Android. She is keenly interested in JavaScript frameworks, such as Bootstrap, Node.js, and AngularJS. She has worked in the IT industry for over 10 years in various positions, such as an application developer, project leader, and trainer. She holds a master's degree in computing from the National University of Singapore and a bachelor's degree in computer engineering from the University of Mumbai. She is certified in security technology and computational neuroscience. She is also the author of the first edition of ASP.NET jQuery Cookbook.

I have enjoyed the journey of writing this book. The process of discovering the magic of jQuery has been enlightening and enthralling. I would like to thank my readers for their valuable feedback that I have incorporated in this edition.

This journey would not be possible without the support of my lovely family. Heartfelt thanks to everyone, especially to my parents, my husband, Aneel, and little, Abraham.

Thanks to the excellent team at Packt and to Priyanka for keeping me going. Special thanks to my reviewer, Ayad Boudiab, for his critical analysis of the material and for providing me valuable insights.

About the Reviewer

Ayad Boudiab is a senior software engineer. He has more than 17 years of experience in application development on Windows platforms. He works across multiple technologies in the .NET stack. In addition to his corporate experience, he has more than 20 years of experience in technical training and teaching. He has taught multiple courses in colleges and online. He has most recently worked with C#, JavaScript, jQuery, ASP.NET, Knockout JS, Angular JS, Kendo UI, HTML5, and SQL Server. You can contact him at <[email protected]>.

Ayad is currently a contractor for a healthcare company in Atlanta. He has reviewed many IT books and written supplements for Wiley, Pearson, nSight, Prentice Hall, among other publishers.

www.PacktPub.com

eBooks, discount offers, and more

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Preface

jQuery is a lightweight JavaScript library that has changed the landscape of client scripting in web applications. Developed by John Resig in 2006, it has taken the Web by storm because of its cross-browser compatibility and its ability to get more done with less code. The library is supported by an active community of developers and has grown significantly over the years. Using jQuery eases many client scripting tasks, such as event handling, embedding animations, writing Ajax enabled pages, among many more, and adds to the interactive experience of the end user. Its extensible plugin architecture enables developers to build additional functionalities on top of the core library.

Learning jQuery and using it in ASP.NET applications is an indispensable skill for ASP.NET developers. This book attempts to impart this skill by exploring diverse recipes for fast and easy solutions to some of the commonly encountered problems in ASP.NET 4.6 applications.

What this book covers

Chapter 1, Getting Started with jQuery in ASP.NET, describes recipes to download and include jQuery in ASP.NET 4.6 Web and MVC applications. It discusses the CDN, NuGet Package Manager, as well as debugging the jQuery code in Visual Studio.

Chapter 2, Using jQuery Selectors with ASP.NET Controls, describes various jQuery selectors that can be used to manipulate ASP.NET controls. These selectors can select controls based on the ID, CSS class, HTML tag, attribute, or position in the document.

Chapter 3, Event Handling Using jQuery, describes recipes to handle different types of events, such as mouse, keyboard, and form events. It also explains event delegation and detaching of events.

Chapter 4, DOM Traversal and Manipulation in ASP.NET, describes techniques to traverse the document, such as accessing parent, child, or sibling elements. It also teaches manipulation strategies to add and remove elements at runtime.

Chapter 5, Visual Effects in ASP.NET Sites, discusses recipes to create different types of animation effects on ASP.NET controls, such as Panel, AdRotator, TreeView, Menu, and GridView. Effects such as enlarging, sliding, and fading are covered in this chapter.

Chapter 6, Working with Graphics in ASP.NET Sites, discusses recipes to work with images and explains effects, such as zooming, scrolling, and fading on images. Utilities such as image gallery, image preview, and 5-star rating control are also explored in this chapter.

Chapter 7, Ajax Using jQuery, explains how Ajax calls can be made to page methods, web services, WCF services, Web API, MVC controllers, and HTTP handlers.

Chapter 8, Creating and Using jQuery Plugins, demonstrates how plugins can be created and included in projects. It also describes how to use the Node Package Manager (NPM) and Bower to download and manage third-party plugins.

Chapter 9, Useful jQuery Recipes for ASP.NET Sites, summarizes the book with diverse recipes to solve common real-world problems. You can find this chapter at: https://www.packtpub.com/sites/default/files/downloads/4836OT_Chapter_09.pdf.

What you need for this book

To work with the examples of this book, you will need the following:

Visual Studio 2015MS SQL Server 2014The Northwind databaseThe jQuery libraryThe jQuery UI libraryA web browserThe Node Package Manager (NPM)Bower

Some recipes also require the use of third-party jQuery plugins, such as validation and cycle plugins.

Who this book is for

This book is for ASP.NET developers who want to use jQuery to write client scripts for cross-browser compatibility. No prior knowledge of ASP.NET or jQuery is expected, and every recipe is self-contained and explained in an easy-to-follow manner. The code samples in this book are provided in both C# and VB. Familiarity with Visual Studio and MS SQL Server is preferred, but not compulsory.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/ASPNET_jQuery_Cookbook_Second_Edition_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books – maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Getting Started with jQuery in ASP.NET

In this chapter, we will cover the following recipes:

Downloading jQuery from jQuery.comUnderstanding CDN for jQueryUsing the NuGet Package Manager to download jQueryAdding jQuery to an empty ASP.NET web project using a script blockAdding jQuery to an empty ASP.NET web project using the ScriptManager controlAdding jQuery to an ASP.NET Master PageAdding jQuery programmatically to a web formUnderstanding the jQuery reference in the default Web Application templateHello World in a web project using jQueryBundling jQuery in ASP.NET MVCUsing a CDN to load jQuery in MVCHello World in ASP.NET MVC using jQueryDebugging jQuery code in Visual Studio

Introduction

As a web developer, you often require to include functionalities in your websites that make writing a client script in JavaScript inevitable. Getting the client script to produce the same response for all browsers has always been a challenge. jQuery helps you overcome this difficulty. In essence, jQuery is a powerful JavaScript library that works across all browsers, such as Internet Explorer (IE), Firefox, Safari, Chrome, Opera, iOS, and Android. It takes away the agony that developers face in order to maintain their client scripts across different platforms.

jQuery is popular not only because of its cross-browser support, but also because it is packed with features that developers can plug and play. It has changed the way developers write a client script. In addition to reducing the amount of code that needs to be written, it provides features for traversing the DOM, event handling, building animations, and AJAX, among many more.

This chapter deals with acquiring the library and other supporting files. It aims to cover different aspects of including and using jQuery in ASP.NET 4.6 web application projects, such as web forms and MVCs.

Note

This book is based on Visual Studio 2015 and jQuery 2.1.4. The scripts have been tested in Internet Explorer 11.0.96, Mozilla Firefox 38.0.1, and Google Chrome 47.0.2526.

If you are familiar with downloading and including jQuery in your ASP.NET applications, you can skip this chapter and move on to recipes for manipulating controls in Chapter 2, Using jQuery Selectors with ASP.NET Controls.

Downloading jQuery from jQuery.com

This recipe explains how to download jQuery on your system along with the version/build to use and the supporting files that are required.

Getting ready

Following are the steps to download jQuery:

Launch any web browser and enter the URL http://www.jquery.com to access the jQuery home page:Click on the Download jQuery button (highlighted in the preceding screenshot) on the right-hand side of the page. This opens up the download page with a list of available files, as shown in the following screenshot:

How to do it…

jQuery is available in two different major versions at the time of writing:

Version 1.xVersion 2.x

Though the Application Programming Interface (API) is the same for both major versions, the difference lies in the support offered for certain browsers. The 2.x line does not support old browsers, such as IE 6, 7, and 8, while the 1.x line continues with this support. So, if the end users of your application will not be using old browsers, you can download the 2.x version.

The jQuery library consists of a single JavaScript (.js) file and can be downloaded in the following formats:

Uncompressed format: This is used in a development environment or when debugging the code.Compressed format: This is used in a production (that is, release) environment. It is compact and uses low bandwidth. It is commonly referred to as the minified version.

To download the file, simply right-click on the required version, 1.x or 2.x, and the required format: uncompressed or compressed. Save the file in a location of your choice as shown in the following screenshot:

Note the following naming convention for the jQuery library:

 

Uncompressed

Compressed

Version 1.x

jquery-1.x.x.js

jquery-1.x.x.min.js

Version 2.x

jquery-2.x.x.js

jquery-2.x.x.min.js

The compressed (minified) version is clearly distinct from the uncompressed version because of the .min.js extension. The minified file uses code optimization techniques, such as removing whitespaces and comments as well as reducing variable names to one character. This version is difficult to read, so the uncompressed version is preferred when debugging.

On the download page, there is also a map file available with the .min.map extension. Sometimes, when bugs appear in the production environment necessitating troubleshooting, the use of the minified file for debugging can be difficult. The map file simplifies this process. It maps the compressed file back to its unbuilt state so that during debugging, the experience becomes similar to using the uncompressed version.

See also…

The Understanding CDN for jQuery recipe.

Understanding CDN for jQuery

A Content Delivery Network (CDN) hosts content for users through large distributed systems. The advantage of using a CDN is to improve the performance. When using a CDN to retrieve the jQuery library, if the files have been downloaded earlier, they will not be re-downloaded. This can help you improve the response time.

How to do it…

The following CDNs are available for jQuery files:

jQuery's CDN provided by MaxCDNThe Google CDNThe Microsoft CDNThe CDNJS CDNThe jsDelivr CDN

To include jQuery on a web page, the URL of the respective CDN can be used so that files can be directly served from the CDN instead of using the local copies. The following table summarizes the respective CDN URLs for jQuery files:

CDN

URL

jQuery's CDN

Version 2.x:

http://code.jquery.com/jquery-2.x.x.js

http://code.jquery.com/jquery-2.x.x.min.js

Version 1.x:

http://code.jquery.com/jquery-1.x.x.js

http://code.jquery.com/jquery-1.x.x.min.js

The Google CDN

Version 2.x:

https://ajax.googleapis.com/ajax/libs/jquery/2.x.x/jquery.js

https://ajax.googleapis.com/ajax/libs/jquery/2.x.x/jquery.min.js

Version 1.x:

https://ajax.googleapis.com/ajax/libs/jquery/1.x.x/jquery.js

https://ajax.googleapis.com/ajax/libs/jquery/1.x.x/jquery.min.js

The Microsoft CDN

Version 2.x:

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.x.x.js

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.x.x.min.js

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.x.x.min.map

Version 1.x:

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.x.x.js

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.x.x.min.js

http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.x.x.min.map

The CDNJS CDN

Version 2.x:

https://cdnjs.cloudflare.com/ajax/libs/jquery/2.x.x/jquery.js

https://cdnjs.cloudflare.com/ajax/libs/jquery/2.x.x/jquery.min.js

https://cdnjs.cloudflare.com/ajax/libs/jquery/2.x.x/jquery.min.map

Version 1.x:

https://cdnjs.cloudflare.com/ajax/libs/jquery/1.x.x/jquery.js

https://cdnjs.cloudflare.com/ajax/libs/jquery/1.x.x/jquery.min.js

https://cdnjs.cloudflare.com/ajax/libs/jquery/1.x.x/jquery.min.map

The jsDelivr CDN

Version 2.x:

https://cdn.jsdelivr.net/jquery/2.x.x/jquery.js

https://cdn.jsdelivr.net/jquery/2.x.x/jquery.min.js

https://cdn.jsdelivr.net/jquery/2.x.x/jquery.min.map

Version 1.x:

https://cdn.jsdelivr.net/jquery/1.x.x/jquery.js

https://cdn.jsdelivr.net/jquery/1.x.x/jquery.min.js

https://cdn.jsdelivr.net/jquery/1.x.x/jquery.min.map

Using CDNs for new releases

Note that CDNs may not have the latest files when new versions of the jQuery library are launched since it usually takes a couple of days for third-parties to update their files. In the case of new releases, always check the available version before downloading them.

How it works…

CDNs consist of servers situated in data centers in strategic locations across the globe. When a client requests a resource from a CDN, the server that is geographically closest to the client processes the request. These servers are also known as edge servers. In addition to this, edge servers have a caching mechanism to serve various assets. All this helps you improve the client's response time.

See also

The Using NuGet Package Manager to download jQuery recipe

Using NuGet Package Manager to download jQuery

NuGet is a package manager available with Visual Studio. It simplifies the process of installing and upgrading packages. This recipe demonstrates the use of NuGet to download the jQuery library.

Getting ready

To launch NuGet for a particular project, go to Tools | NuGet Package Manager | Manage NuGet Packages for Solution... as shown in the following screenshot:

Alternatively, right-click on the project in the Solution Explorer tab, and select Manage NuGet Packages.

How to do it…

Perform the following steps to download jQuery using NuGet Manager:

In the NuGet Package Manager, as shown in the following screenshot, select the jQuery package from the left-hand side panel. In the right-hand side panel, select the Version that you would like to use in your web project from the drop-down menu. Click on the Install button:

Tip

Searching for packages in NuGet

If jQuery is not visible in the left-hand side panel, you need to search for it by keying in jQuery in the search box in the top left corner of the NuGet Manager screen.

Click on OK when prompted for confirmation in order to make the required changes to the solution.

How it works…

The NuGet Package Manager downloads the selected version of jQuery in the Scripts folder. Any other version existing in the Scripts folder is deleted. The Scripts folder will look like the following screenshot:

The files downloaded by NuGet are as follows (the version numbers may change in the future):

The Intellisense file: jquery-2.1.4.intellisense.jsThe debug version : jquery-2.1.4.jsThe release version: jquery-2.1.4.min.jsThe map file: jquery-2.1.4.min.map

See also

The Downloading jQuery from jQuery.com recipe

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

To create ASP.NET 4 .6 Web Applications, Visual Studio provides various ready templates such as Empty, Web Forms, MVC, Web API, and so on. This recipe will use the Empty template, which provides the developer with an empty project structure that consists of only the web.config file.

Tip

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Getting ready

Following are the steps to create a project by using Empty template:

Create a new project in Visual Studio by going to File | New | Project..., as shown in the following screenshot:

Tip

Website or web project?

Instead of creating a new project, you can also create a new website. Unlike a project, a website does not contain a collective project file to track individual files in the application. To create a website, go to File | New | Web Site.... This will launch the New Website dialog box with the list of available templates. Select the ASP.NET Empty WebSite template.

This will launch the New Project dialog box, as shown in the following screenshot. From the left-hand side panel, select your desired programming language, Visual C# or Visual Basic, and then, select ASP.NET Web Application from the middle panel:Enter WebApplication1 (or any suitable name) in the Name field. Click on the Browse button to go to the desired Location where you would like to save the application. Click on OK.This will launch the Select a template dialog box, as shown in the following screenshot:FromASP.NET 4.6 Templates, select Empty, and click on OK. Visual Studio will create an empty project in the Solution Explorer tab, as shown in the following screenshot:

Note

In the remaining recipes, when asked to create a Web Application project using the Empty template, follow the steps listed in this section.

How to do it…

Following are the steps to include jQuery using script block:

JavaScript files are usually placed in a folder named Scripts in the web application. So, in the Solution Explorer tab, right-click on the project and go to Add | New Folder from the menu:Rename the folder to Scripts. Now, right-click on the Scripts folder, and go to Add | Existing Item... as shown in the following screenshot:Now, browse to the location where you have saved the downloaded copy of the jQuery files (refer to the Downloading jQuery from jQuery.com recipe), and click on OK. It is recommended that you add both the uncompressed and compressed versions. The Scripts folder will be updated, as shown in the following screenshot:Next, create a new web form in the project by right-clicking on the project and navigating to Add | New Item.... From the dialog box, select Web Form, and enter a suitable name for the web form, such as Default.aspx:To use jQuery on the web form, simply drag and drop the required jQuery file, that is, uncompressed or compressed on the web form. Or alternatively, include the following <script> tag in the <head> element:

For development mode, the code is as follows:

<script src="Scripts/jquery-2.1.4.js"></script>

For release mode, the code is as follows:

<script src="Scripts/jquery-2.1.4.min.js"></script>

See also

The Downloading jQuery from jQuery.com recipe

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

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).Add a new Web Form to the project and name it Default.aspx.Add the jQuery library files to the Scripts folder.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:

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

Understanding jQuery reference in the default web application template

So far, all examples have used the Empty template for