Click here to Skip to main content
6,293,171 members and growing! (11,743 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Beginner License: The Code Project Open License (CPOL)

How to easily use jQuery DatePicker in ASP.NET

By Sowkot Osman

How to easily use jQuery DatePicker in ASP.NET
Javascript, CSS, HTML, XHTML, ASP.NET, Dev
Posted:11 Aug 2008
Updated:20 Aug 2008
Views:15,011
Bookmarked:24 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
7 votes for this article.
Popularity: 2.84 Rating: 3.36 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
1 vote, 14.3%
3
2 votes, 28.6%
4
2 votes, 28.6%
5

Introduction

JQuery is an excellent javascript library to build modern user interactive website.
Its very easy to use jQuery in any web application. jQuery has a strong set of javascript UI like datePicker, Tab Panel etc.

I've written a small Utility class to use jQuery datePicker in ASP.NET and publishing it in this post.
Its very simple to use this class. You just need to pass the Page object and TextBox of your page.

Screen Shot

ScreenShot.jpg

Using the code

See the following Util Class.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Util
{
    public class JQueryUtils
    {
        public static void RegisterTextBoxForDatePicker(Page page, params TextBox[] textBoxes)
        {
            RegisterTextBoxForDatePicker(page, "dd-mm-yy", textBoxes);
        }
        public static void RegisterTextBoxForDatePicker(Page page, string format, params TextBox[] textBoxes)
        {
            bool allTextBoxNull = true;
            foreach (TextBox textBox in textBoxes)
            {
                if (textBox != null) allTextBoxNull = false;
            }
            if (allTextBoxNull) return;
            page.ClientScript.RegisterClientScriptInclude(page.GetType(), "jquery", "JQuery/jquery.js");
            page.ClientScript.RegisterClientScriptInclude(page.GetType(), "jquery.ui.all", "JQuery/ui/jquery.ui.all.js");
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "datepickerCss", "<link  rel=\"stylesheet\" href=\"JQuery/themes/flora/flora.datepicker.css\" />");
            StringBuilder sb = new StringBuilder();
            sb.Append("$(document).ready(function() {");
            foreach (TextBox textBox in textBoxes)
            {
                if (textBox != null)
                {
                    sb.Append("$('#" + textBox.ClientID + "').datepicker({dateFormat: \"" + format + "\"});");
                }
            }
            sb.Append("});");
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "jQueryScript", sb.ToString(), true);
        }
    }
}  

The usage is very simple. If you have a TextBox named MyDateTextBox in your .aspx page then you have to write following line
in the Page_Load event to attach the TextBox with jQuery.

 protected void Page_Load(object sender, EventArgs e)
 {
      Util.JQueryUtils.RegisterTextBoxForDatePicker(Page, MyDateTextBox); 
 }

The second parameter takes variable arguments. So you can pass any number of TextBox to the function.
There is also an overloaded function to take your desired date format.

You must download the jQuery library from jQuery UI site http://ui.jquery.com/download

You can only download the files for datePicker. But remember to rename the .js files in My JQueryUtils class.
You can see I've written the jQuery core library js file name, .ui.all.js file and the .css file for datePicker.

I've attached a sample project hereby. See it.
Cheers!

My Other Articles

*) Common Validation and Length Validation with Regular Expression in ASP.NET

*) How to easily use jQuery DatePicker in ASP.NET

*) Generating Unique Key(Finger Print) for a Computer for Licensing Purpose

*) How solving ACM problems is helpful to develop quality software

*) Insertion problem with ASP.NET AccessDataSource

*) A simple method to compare versions

*) How to show a pdf in landscape mode using iText API in Java

*) Properly display paragraphs in browser

*) Mounting NTFS partition in Debian

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sowkot Osman


Member
3 years of extensive experience in devloping both desktop and web applications in C#.NET, VB.NET and ASP.NET
2 year of experience in developing web application in J2EE, Free Marker, JSP, Spring MVC, Spring Webflow.
Expertise in Software Architecture and Framework design.
Blog: http://sowkot.blogspot.com


Occupation: Software Developer
Location: Bangladesh Bangladesh

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralUse the text Pinmemberprpleprncs20:15 2 May '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Aug 2008
Editor:
Copyright 2008 by Sowkot Osman
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project