July 04 2009

Setup your website for using jQuery and jQuery plugins

Introduction

The moment jQuery appeared on web, everyone started using it. Specially now a days ASP.NET guys use it more and more. I love to use jQuery and jQuery plugins for my ASP.NET web sites. It made my work fun and exciting.

Before jQuery and plugins, I was a bit scared to touch JavaScript. jQuery really made an entire boring and scary thing interesting for me and many others.

So often we need to setup our web site for using jQuery and some plugins.  I am going to show you some ways how to set your web site for that.

Step 1 – Download jQuery

Always download the latest jQuery. Because this is my personal experience that with every release they improve performance and fix some bugs. get the latest jQuery from www.jquery.com.

Many of us use Visual Studio 2008 for development and “Intellisense” is our basic need. So to get intellisense, always download the .vs-doc file too. Its a documentation file for jQuery. If you will have this in your project, you will get intellisense for jQuery also. You will get all these files from download section of www.jquery.com. But remember you need to install VS Hotfix for that intellisense to work. Check this post for more information

Step 2 -Setup your web site

Now I have downloaded latest scripts. I have added folder called “Scripts” to root of web site. I will use same folder for jQuery plugins.

Scripts

Next step is to reference scripts in my master page ( I will use master page for my web site ). Some people prefer to directly add code to head section of master page. But I prefer to reference my scripts by “ScriptManager” control. It has many advantages. Like scripts file will be managed by scriptmanager, which is also taking care of other scripts.

Now I will add a ScriptManager to my master page. Its always good to have ScriptManager in your MasterPage, so that all pages in your web site will get it automatically,and you do not need to add it page by page. Next I will add a reference to my jQuery file to ScriptManager

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="MssterPages_Site" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="myScriptManager">
</asp:ScriptManager>
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

There are some other options  also in the script tag, which we will discuss later, but for now this seems good. When you will deploy your web site with debug=”false”, in your web.config file. ScriptManager will take care of your scripts automatically. The script will be cached for better performance. So now we have core jQuery set in our web site.

Step 3 -set any plugin for site wise use

Now  lets use a plugin called as “Block UI”. We will use this plugin to block the page and show a waiting message, whenever there is some AJAX activity. Check more about this very useful plugin here

Scripts_plugin

And also I have updated ScriptManager to include this plugin.  I need to do this, so that plugin will be avalable to all of my web site.


<asp:ScriptManager runat="server" ID="myScriptManager">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.3.2.js" />
<asp:ScriptReference Path="~/Scripts/jquery.blockUI.js" />
</Scripts>
</asp:ScriptManager>

I have added some code to master page to use Block UI. Check the follwing code.

<script language="javascript" type="text/javascript">
$(document).ready(function() {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(onRequestStart)
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onRequestEnd)
});
function onRequestStart() {
$.blockUI();
}
function onRequestEnd() {
$.unblockUI();
}

</script>

This code will block the UI when AJAX will start and unblock it, when AJAX is completed

Step 4: Use a plugin for single page
There are some cases, where you need to use a jQuery plugin just for a single page. In that case adding a reference to ScriptManager on master page is not a good idea. For such a cases, I prefer to
use ScriptManagerProxy and add reference to it. This will be advantage that my plugin will be served by ScriptManager on master page, only when it has child page as the desired one. So lets do it.

Lets say I need to print a dataform on a page. The best choice for this will be using jqPrint plugin, which can print portion of page. check about it here. I have downloaded the plugin and placed it in my scripts folder

Scripts_plugin_2

I need this on page “PrintData.aspx”. I will include a ScriptManagerProxy on this page and will add a reference to it for jqPrint plugin script.


<%@ Page Title="Print this page" Language="C#" MasterPageFile="~/MasterPages/Site.master"
AutoEventWireup="true" CodeFile="PrintData.aspx.cs" Inherits="PrintData" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ScriptManagerProxy ID="myScriptManagerProxy" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery.jqprint.js" />
</Scripts>
</asp:ScriptManagerProxy>
</asp:Content>

Step 5 : Test It
Now lets test our web site. Execute the default.aspx page. Now if you inspect your page with firbug for script, you will see something like this -

default

So  there is only jQuery core and Block UI scripts. Next execute the PrintData.aspx page and inspect it with FireBug, you will see something like this -

PrintData

Now you will see that for this page, you will also get the jQprint script. This is because we have specified that we will use this only on this page. Check attached code for more information.

Have fun with jQuery :)

Attached Files:

Comments:

(13) posted on Setup your website for using jQuery and jQuery plugins

Post a comment

Spam protection by WP Captcha-Free

RSS