Create new account I forgot my password    

Using jQuery with ASP.NET - A Beginner's Guide
Rating: 66 user(s) have rated this article Average rating: 3.9
Posted by: Suprotim Agarwal, on 11/18/2008, in category "jQuery and ASP.NET"
Views: this article has been read 82225 times
Abstract: This article is a guide for getting started with jQuery using Visual Studio 2008. The article also shows you how to use Visual Studio 2008 intellisense feature to develop jQuery and ASP.NET applications.

Using jQuery with ASP.NET - A Beginner's Guide
 
Did you hear about jQuery.. NO...? It’s fantastic!! Well if you have been hearing those words more too often from your colleagues but haven’t had the chance to explore jQuery yet, then here’s this beginner guide for you to get started with jQuery and ASP.NET.
Update: I have written an EBook on using jQuery with ASP.NET Controls. It's called "51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls".
Ok, the million dollar question - What is jQuery anyways?
jQuery is a fast, lightweight JavaScript library that is CSS3 compliant and supports many browsers. The jQuery framework is extensible and very nicely handles DOM manipulations, CSS, AJAX, Events and Animations.
What is the difference between JavaScript and jQuery?
JavaScript is a language whereas jQuery is a library written using JavaScript.
Where can I download jQuery?
The latest version of jQuery as of this writing is jQuery 1.2.6 and can be downloaded from here. [Update] An updated version of jQuery 1.3.2 has been released. jQuery 1.3.2 and jQuery 1.3.2 Visual Studio 2008 Autocomplete documentation. So whereever you see 1.2.6 mentioned in the rest of this article, just replace it with 1.3.2.
What has Microsoft got to do with jQuery?
In ScottGu’s blog, there was an announcement made a few weeks ago that Microsoft will be partnering with the jQuery team and shipping jQuery with Visual Studio in future. Also, jQuery intellisense annotation support will be available as a free web-download. Barely a few weeks after the announcement, Microsoft during the PDC event (held in the last week of October), announced that Visual Studio 2008 now supports jQuery Intellisense through an additional file available from jQuery. This file can be downloaded from here. For those interested, the release notes can be found here 1.2.6 (Release Notes).
A few days ago, Microsoft also released a VS2008 SP1 Hotfix to support all JavaScript files including jQuery intellisense for Visual Studio 2008. Note that this hotfix works only if you have VS 2008 with SP1 or Visual Web Developer Express Edition with SP1. You can download the Hotfix from here (Downloads tab).
Checking out jQuery Intellisense in Visual Studio 2008 with SP1
Assuming you have installed the hotfix , downloaded the jQuery library and the jQuery VS 2008 IntelliSense documentation, follow these steps to move ahead.
Open Visual Studio 2008 > File > New > Website > Choose ‘ASP.NET 3.5 website’ from the templates > Choose your language (C# or VB) > Enter the location > Ok. In the Solution Explorer, right click your project > New Folder > rename the folder as ‘Scripts’.
Right click the Scripts folder > Add Existing Item > Browse to the path where you downloaded the jQuery library (jquery-1.2.6.js) and the intellisense documentation (jquery-1.2.6-vsdoc.js) > Select the files and click Add. The structure will look similar to the following:
jQuery Structure
Now drag and drop the jquery-1.2.6.js file from the Solution Explorer on to your page to create a reference as shown below
jQueryRef
Note: Since you have applied the hotfix, you do not have to manually add a reference to the jquery-1.2.6-vsdoc.js file in your page. Once the reference to the runtime library has been added, Visual Studio automatically searches for the ‘vsdoc’ file and loads it. You just need to keep both the runtime library and the documentation file next to each other.
To test intellisense, add a <script> block and key in ‘$(‘. You will get an intellisense similar to the one shown below:
jQuery Intellisense
Show me some examples
This article would be incomplete without examples. So let us quickly see some.
Example 1 – Display an alert on asp:Button click using jQuery
Add a Button element to the page as shown below:
    <asp:Button ID="Button1" runat="server" Text="Button" />
Now in order to tell the browser to perform some action using jQuery as soon as the document is ready or loaded, use this block:
    <script type="text/javascript">
        $(document).ready(function() {
        // add code here
        });
    </script> 
Add your code in the function block
    <script type="text/javascript">
        $(document).ready(function() {
        $("#Button1").click(function() {
            alert("Hello world!");
        });
 
        });
    </script>
On clicking the button, you get an alert code similar to the following:
jQuery Hello World
Example 2: Demonstrating some animation on button click using jQuery
Our first example was a simple ‘Hello World’ example to keep up with the tradition while introducing a new language. Let us enhance our sample and display some animation using jQuery. We will animate an <asp:Panel> on a button click when the page loads. This sample is loosely based on the demo over here:
To do so, add a HTML button and a <asp:Panel> to the page as shown below:
<form id="form1" runat="server">           
        <input id="btnAnimate" type="button" value="Animate" />
        <asp:Panel ID="Panel1" runat="server">
        Some sample text in this panel       
        </asp:Panel>
    </form>
Also add some css to beautify the div (remember asp:Panel renders as a <div>). The <style> tag has been kept in the <head> element of the page.
    <style type="text/css">
        div {
        background-color:#D5EDEF;
        color:#4f6b72;
        width:50px;
        border: 1px solid #C1DAD7;
 
      }
 </style>
Finally add the jQuery code to animate the panel on button click. We would be using the ‘animate’ function. Observe how the intellisense helps us out with the parameters
jQuery Intellisense VS
After adding in the parameters for the animate() function, the complete code will look similar to the following:
<script type="text/javascript">
     $(document).ready(function() {
         $("#btnAnimate").click(function() {
             $("#Panel1").animate(
            {
                width: "350px",
                opacity: 0.5,
                fontSize: "16px"
            }, 1800);
         });
     });
 </script>
Here the animation occurs while changing the width, opacity and font properties of the Panel when the button is clicked. The value ‘1800’ represents the number of milliseconds to run the animation.
Run the sample and see the Panel getting animated as shown below:
jQuery Animation
jQuery Animation
Well that’s it for an introduction. Remember that this article is only an introduction and in no way demonstrates the capabilities of jQuery.  I have written an EBook which demonstrates the capabilities of doing client side development in ASP.NET using jQuery. It's called "51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls". In the forthcoming articles, we will see how jQuery can further be used with ASP.NET and AJAX.
If you liked the article,  Subscribe to my RSS Feed.







Follow me on twitter

Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Lineker Tomazeli on Tuesday, November 18, 2008 8:18 AM
Thanks man... Good explanation
Comment posted by Pitra Kanjz on Wednesday, November 19, 2008 1:00 AM
I always think to use jquery but did not find how. Now i will..(pardon my english) very very good.
Comment posted by Kumar on Saturday, November 22, 2008 2:00 AM
Hi,
Thanks a lot for great article . I have been trying to learn it from past few days,and this article clears all my doubts.
Simple way to explain what jquery is .Hope we get more article on jquery in near future.

Thanks
Kumar
Comment posted by Suprotim Agarwal on Sunday, November 23, 2008 10:14 PM
Pitra, Kumar: Thanks. You will be seeing a lot of jquery stuff soon. Stay Tuned!
Comment posted by sudarshan on Wednesday, November 26, 2008 4:11 AM
very nice article, espically for beginners. Thanks a lot.
Comment posted by Killer on Thursday, December 04, 2008 6:48 PM
This article does a poor job at explaining for beginners why they should use jQuery. What advantage does it have over other things and why is it so great?

A comparison of old way to new way would be more obvious for the beginner. Author does not give this.

It seems that jQuery just gives you something more difficult to refactor or manage.
Comment posted by Suprotim Agarwal on Friday, December 05, 2008 10:25 AM
Killer: Thanks for your comments. The intension of the article is to introduce jQuery from a Visual Studio 2008 perspective and how Microsoft is taking initiatives to provide users with a smooth experience.

A detailed Introduction to jQuery with benefits and examples can be found over here:  http://jquery.com/
Comment posted by Rajendra on Saturday, December 27, 2008 8:24 AM
Dear
I am unable to download VS2008 SP1 hotfix. Can U email me the script as a zip.
Thanks
Comment posted by Suprotim Agarwal on Tuesday, December 30, 2008 3:16 AM
Rajendra: I just checked the link again and was able to download the hotfix without any issues. Please could you try again.
Comment posted by Anisha on Friday, January 23, 2009 12:02 AM
Nice article .JQuery seems to be vey interesting
Comment posted by kevin on Saturday, January 24, 2009 6:57 PM
the thing is when the server control is within a template, the ID of each item is dynamically created.
the $('#serverControlID') becomes not that useful. The problem can be solved using > or class selectors. But there are times you have to write more codes in order to make it work with asp.net. I am very glad MS finally decide to include JQuery as part of VS. Can wait for the final product.
Comment posted by Morad Farshid on Thursday, January 29, 2009 5:43 PM
For someone just starting with jquery and wishing to use it with ASP.NET this artilce is a must read. Excellent. Everything works as described.
Comment posted by dustin on Friday, February 06, 2009 6:08 PM
Nyce, but you should really WARN the newbees here about the uselessness of Jquery in msft's gawd-awful postbacks...

document.ready things are GONE when you post back.

I love JQuery but sheesh! what a pain to use it with asp.net in the real world!
Comment posted by Jerad on Tuesday, February 10, 2009 3:15 PM
How are you able to use the asp control ID's in the jquery code?  

It only works for me when I either use the generated id (eg ctl00$ContentPlaceHolder1$button1), or some other workaround like button1.ClientID
Comment posted by Jerad on Tuesday, February 10, 2009 3:17 PM
"How are you able to use the asp control ID's in the jquery code? "

It works as in the example when you don't use a master page (my site uses them, and I always have, so if you use one, I guess you'll have to find a workaround, bummer)
Comment posted by qis on Thursday, February 12, 2009 9:59 AM
I installed Hotfix and alsp sp1 foe visual web developer express 2008 and .Net framework. I am getting the intellisence but not able to make your code work. is there any thing i am missing.

BR
Comment posted by Suprotim Agarwal on Thursday, February 12, 2009 11:00 AM
Jerad: Here's an article that should help you out: http://john-sheehan.com/blog/custom-jquery-selector-for-aspnet-webforms/

qis: Can you use the Contact form and mail me the code. I will have a look at why it ain't working.
Comment posted by anoop on Saturday, February 14, 2009 2:10 AM
can we use this in visual studio 2005
Comment posted by anoop on Saturday, February 14, 2009 2:11 AM
can we use this in visual studio 2005
Comment posted by cetchells on Saturday, February 14, 2009 6:58 AM
Small note, but if you download the compressed version of jquery with the .min.js extension, remove the .min part of the file name. Then it works.
Comment posted by Dave Hanna on Thursday, February 26, 2009 10:48 AM
The Hotfix for Intellisense support apparently does not apply to the Visual Studio Team System.  (When I try to install it,it says "None of the products addressed by this update are installed on this computer."  And the Knowledge base article says it applies only to Visual Studio 2008 and Visual Web Developer 2008 and not to any other version of VS.)
Do you know if there is a way to get intellisense support in the Team System?
Comment posted by mayuresh on Wednesday, March 04, 2009 4:08 PM
Adding the same code to a child page inheriting a master page..cause the page to referesh on postback

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

    <script src="../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

        $(document).ready(function() {

        $("ctl00$ctl00_ContentPlaceHolder1_btnShowAlert").click(function() {
            alert("Show Hello");
            })

        });        
        
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <br />
    <asp:Button ID="btnShowAlert" runat="server" Text="Show Alert" />
    
</asp:Content>
Comment posted by Tan on Friday, March 06, 2009 5:40 AM
Thanks a lot.
Very simple and easy to understand. Presented nicely.
Tan
Comment posted by Harold Chattaway on Saturday, March 14, 2009 10:34 PM
I have just confirmed also that the Jquery intellisense does not work in Team system Development edition as noted by Dave Hanna. It does seem to work if you have a separate .js file with your script code and place a "/// <reference path="jquery.js" />" in the top of the code. This hooks up the Intellisense file. But Intellisense does not work in a aspx page! PITA! Works fine in VS 2008 sp1 Professional though.

Comment posted by Bino on Monday, March 23, 2009 11:45 PM
Why do we need to use JQuery, if just an alert requires these many lines of coding, and even the syntax is entirely different. I dont know y people are creating things based on existing items with different syntax as they wish.
Comment posted by Suprotim Agarwal on Tuesday, March 24, 2009 3:00 AM
Bino: As mentioned in the article, "Well that’s it for an introduction. Remember that this article is only an introduction and in no way demonstrates the capabilities of jQuery. " ..Did you check out the other articles or the jQuery site http://jquery.com to see what jQuery can do?
Comment posted by Avinash on Wednesday, April 08, 2009 8:36 AM
I followed the above steps, however I am not getting intellisense, any idea why?
Comment posted by avinash on Wednesday, April 08, 2009 8:43 AM
when I install the hotfix, its showing this error:

"None of the products that are addressed by this software update are installed on this computer. Click Cancel to exit setup."
Comment posted by abhilashca on Wednesday, April 29, 2009 6:47 AM
Nice post to start jQuery.
Really simple.
But, even after installing the hotfix, I's not able to get the intellisense.
Thanks.
Comment posted by Azad on Thursday, April 30, 2009 7:07 AM
Thanks,It's really helpful for beginners.
Comment posted by wozi on Thursday, May 07, 2009 10:02 AM
Avinash, do you have 1.3.2 version? If yes, change file name
jquery-1.3.2-vsdoc2.js
to:
jquery-1.3.2-vsdoc.js
Comment posted by hoangthangdl on Thursday, May 21, 2009 1:51 PM
very good...
Comment posted by Michael on Thursday, July 23, 2009 1:33 AM
I use VS Team System 2008 SP1  (Version 9.0.30729.1 SP) and everything is working great for me.
Comment posted by Sudhanshu Tripathi on Friday, July 31, 2009 7:35 AM
<script type="text/javascript" src="Script/jquery-1.2.6.js">
<script type="text/javascript">
        $(document).ready(function() {
        $("#Button1").click(function() {
            alert("Hello world!");
        });

        });
    </script>

<asp:Button ID="Button1" runat="server" Text="Button" />

The above code is not executing on button click.
how to show alert messgae on button click using jquery
Comment posted by hai on Monday, August 10, 2009 1:29 AM
<script type="text/javascript">
  alert("Hello world!"); </script>
Comment posted by Manoj Bisht on Monday, October 19, 2009 10:02 AM
Excellent article for beginner's but i'm unable to download VS2008 SP1 hotfix. The link is n't at work. Please let me know where i can download this one
Comment posted by Banai Gurjar on Wednesday, November 25, 2009 11:20 AM
you should not use to difficult way to jquery,because all of beginner is no brilent mind so they easaly get it and implement,,so i hope you will introduce a normal way to jquery in next implementation
Thanks
Comment posted by Neeraj on Wednesday, December 16, 2009 4:25 AM
Hi dear,

Great explanation.
Comment posted by Konrad on Tuesday, January 05, 2010 10:23 AM
Something important when referencing server controls,
if you have a asp:Button control, $("#Button1") won't work.
You need to use this $("#<%= Button1.ClientID %>")
Comment posted by Sarnendu De on Wednesday, January 06, 2010 12:42 AM
Hi Konrad,
$("#Button1") is working fine both in Visual Studio 2008 and 2010 Beta. In general it is working for server controls also.
Thanks
Sarnendu De
Comment posted by Sarnendu De on Wednesday, January 06, 2010 1:02 AM
Hi Sudhanshu,
Code you posted is working fine.Please check js file in src is in correct folder.
Same code like your but in visual studio 2010 Beta, works in 2008 also:
<script type="text/javascript" src="Scripts/jquery-1.3.2.js"></script>
<script type="text/javascript" src="Scripts/jquery-1.3.2.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#Button1").click(function(event)
  {
    alert("Welcome to jQuery");
  });
});
</script>
<asp:Button ID="Button1" runat="server" Text="Button" />
Thanks
Sarnendu De
Comment posted by Sarnendu De on Wednesday, January 06, 2010 1:10 AM
In Visual Studio 2008, we need to add a folder Scripts and add javascript file but in Visual Studio 2010 Beta it is integrated and Scripts folder with js files jquery-1.3.2.js,jquery-1.3.2.min.js,jquery-1.3.2-vsdoc.js will be there.
Thanks
Sarnendu De
Comment posted by Rita V on Wednesday, January 13, 2010 5:46 PM
I have visual studio 2008 SP1 and I have also downloaded  jquery-1.2.6.js,
jquery-1.2.6-vsdoc.js and hotfix. Following your sample,the alret message doesn't pop up?
What else I need to do?
Comment posted by Rita V on Wednesday, January 13, 2010 5:51 PM
I downloaded jquery-1.3.2.js version and it is working fine.
Thanks!
Comment posted by karl on Tuesday, January 26, 2010 2:55 AM
nice article,you teached me how to use jquery. :) thanks a lot!
Comment posted by gdgfdgfd on Monday, February 08, 2010 1:45 AM
nice article,you teached me how to use jquery. :) thanks a lot!

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel