Getting started with jQuery and ASP.NET
As promised, I am starting a series on jQuery. Today our goal is to get a project setup in Visual Studio that uses jQuery. We won’t be explaining much in the way of syntax, but we will get our first “hello world” application up and running.
To start off, the first thing you’ll want to do is to create a new project. Once you’ve finished this example you’ll have a pretty good idea of how to add jQuery to an existing project. So even if your goal is to add jQuery to an existing project, for now just create a new project and follow along.
Once you’ve created the new project, the first thing you’ll want to do is to create a new subdirectory for the jQuery javascript file. I just called my directory “js.”
Once you’ve created the directory, go ahead and download the jQuery library from www.jquery.com. You can either download it directly into the js directory we created or you can copy it into that directory once you’ve completed the download.
Since you will probably want jQuery available to you in all of the files you will be using, I suggest that you create a Master Page for this project. If you are using Visual Studio 2008, the Master Page will have two content sections in it–one in the head and another in the body. You’ll want both.
Drag and drop the jQuery js file into the head section of the master page. This will automatically create the
<script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
code that will allow the jQuery library to be loaded.
The next thing we will want to do is to delete the default.aspx(.cs/.vb) file that was created for us when we created the project and create a new one that uses the Master Page we just created.
The final thing we will want to do is to create a javascript file that can be used by this Default file. I would recommend a separate javascript file for each ASPX file (or HTML file) in your application. You can either keep them under the js directory, or in the same directory as the ASPX file. Personally, I think it will make things easier to find if you put the JS file in the same directory as the ASPX file and give it the same name as the ASPX file. That is, Default.aspx will have a js file named Default.js.
Once you’ve created that file, add this code snippet to it.
$(document).ready(function() { alert("jquery is working"); });
Don’t worry about what it does. For now all you have to know is that the application will display “jquery is working” in a message box when we run the default.aspx page if everything is working correctly.
The last step is to add the default.js file to the Default.aspx page. We can do that by dragging and dropping the default.js file into the ContentPlaceHolder control that replaces the content in the head of the Master Page.
If you’ve done everything correctly, you can run the default.aspx page and a dialog will display “jquery is working” once the page has loaded.
Other post in jQuery
- jQuery - The Man, The Myth, The Legend - October 8th, 2008
- Getting started with jQuery and ASP.NET - October 15th, 2008
- jQuery - Explaining Last Week's Code - October 21st, 2008
- Friday Books - "Learning jQuery" - October 24th, 2008
- jQuery Simple Selectors - October 28th, 2008
- Friday Books - "jQuery in Action" - October 31st, 2008
- jQuery Selectors - Looks just like CSS - November 6th, 2008
- VS2008 SP1 Hotfix to Support "-vsdoc.js" IntelliSense Doc Files - November 11th, 2008
- jQuery Looks like XPath - November 12th, 2008
- jQuery - class manipulation - November 19th, 2008
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!




















































October 16th, 2008 at 8:29 am
[...] Getting Started with jQuery and ASP.NET (Dave M. Bush) [...]