Integration of jQuery DatePicker in ASP.NET Website (part 1)

In the previous days, I’ve been working on few jQuery DatePicker plug-ins.

The one I will be using in this blog post is the Keith Wood’s DatePicker plug-in which has a lot of features that can be easily extended. The source code of the plug-in you have it here, including some demos and examples, but also in the end of this blog post, I have posted link to the complete project on which I’m integrating this plug-in with some changes I have done so far.

Firstly, I would like to note that this plug-in is fully compatible with jquery-1.4.2.min.js, so that we can easily reference the link from the Microsoft Ajax CDN

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>

- Files in the Project


We have only one javascript jquery.datepick.js which is the plug-in itself.

Also, there are two CSS files:

  • jquery.datepick.css - which comes together with the plug-in
  • style.css - simple stylesheet that I have made to override some plug-in styles and formatting

And, one Default.aspx page where I'm doing the complete integration of this plug-in.

 

- Complete ASPX code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!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">
<
link rel="Stylesheet" type="text/css" href="styles/jquery.datepick.css"></link
>
<
link rel="Stylesheet" type="text/css" href="styles/style.css"></link
>
<
script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" type="text/javascript"></script
>
<
script type="text/javascript" src="scripts/jquery.datepick.js"></script
>
<
script type
="text/javascript">
$(function() {
$('#startdate').datepick({ dateFormat: 'dd/mm/yyyy' });
$('#enddate').datepick({ dateFormat: 'dd/mm/yyyy' });
});
</script
>
</
head
>
<
body
>
<
form id="form1" runat
="server">
<
div id
="content">
From <asp:TextBox ID="startdate" class="field" runat="server"></asp:TextBox> -
To <asp:TextBox ID="enddate" class="field" runat="server"></asp:TextBox
>
</
div
>
</
form
>
</
body
>
</
html
>

Shortly, I have referenced the scripts and styles. There are two text boxes with ids: startdate and enddate. Also, these text boxes contains class=”fields” which is one of the classes in the style.css file.

But nothing would work as expected if we don’t have the six lines of jQuery code which do all the magic in our integration:

    <script type="text/javascript">
$(function() {
$('#startdate').datepick({ dateFormat: 'dd/mm/yyyy' });
$('#enddate').datepick({ dateFormat: 'dd/mm/yyyy' });
});
</script
>

 

The result is


I have changed the default date format which is ‘mm/dd/yyyy’ to ‘dd/mm/yyyy’ just to test the way of how to manipulate the plug-in settings.

Here you can find the complete list of settings that this plugin supports: http://keith-wood.name/datepickRef.html

You can also easily modify the plug-in to localize in your local language, it has regional settings, part of the code:

this.regional = {
'': {
// US/English
monthNames: ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
dayNamesMin: ['Su', 'Mo', 'Tue', 'We', 'Thu', 'Fr', 'Sa'],

Note: In one of the following blogs, I will cover an example on how to make complete localization of this plug-in in your own language, even though it’s already translated in most of the languages right now.

 

Few of the settings I have tested so far:

1. Animation of the calendar: the default animation the plug-in uses is ‘show’ – you can override it with any animation supported in jQuery

Example:

$('#startdate').datepick({ dateFormat: 'mm/dd/yyyy', showAnim:'fadeIn' });

 

2. Show speed of the calendar: the default is set to ‘normal’ – you can override it to fast or slow

Example:

$('#startdate').datepick({ dateFormat: 'mm/dd/yyyy', showSpeed: 'fast' });

3. minDate and maxDate: you can easily specify what should be the minimum and maximum date a user can select

$('#startdate').datepick({ minDate:'07/01/2010', maxDate:'07/15/2010' });

You see, the half of the month days are grayed because I have set the maxDate to 15 July 2010. Also, when clicking the Months dropdownlist, in this case there would be no month because I've set minDate to 1 July 2010.

So, this is the main way to integrate this interesting jQuery plug-in into an ASP.NET WebForms Website. The way does not differs much in MVC because we do almost everything in jQuery.

In the next blog, I will post some more examples where I will use some validation scripts I’ve written and do some post backs.

Hope this was interesting for reading.

Here is the complete source code so far. The next time I will post the source code with the modifications I will do.

Please write your comments and suggestions :).

Thank You,
Hajan

PART 2 - JS VALIDATION SCRIPT

28 Comments

  • Hi Hajan,
    Great post! I've implemented your date picker through and through. However, I have an issue and was wondering if you could enlighten me.

    On the same control that I have the date picker, I also have an ajax update panel, with sync as well as asych triggers. The date pick works before I trigger any ajax, and doesn't work after. Any clue of a workaround here?

    Thanks again.

  • I can't set
    $('#startdate').datepick({ minDate:'07/01/1980', maxDate:'07/15/2100' });
    value select year.

  • DatePicker not working in IE 8 but it is working in IE 6 and mozilla so pls check it out. I have check it in IE compatibility mode but it not working. So pls give me some sol.

  • @Deepak, it works fine in all IE 6+ versions. Create new clean web site and test the code... If it's not working within your application, the reason might be something else. If you can't get it working, then try the jQuery UI Datepicker...

  • Hello,
    Is there a setting or a way where I could enable weekend selection. Right now if you click on a weekend, the highlight goes away. I need to be able to select the weekend dates as well.

    thanks

  • @Param,

    If I understand your question well, you want to be able to select Saturday and Sunday days? I think it should work.

    Otherwise if you want to select all the days in a week, you can use range selection by adding rangeSelect:true property... You can also stylize your datepicker to show the weeks - check the example in the following link: http://keith-wood.name/datepick.html#layout scroll down a bit.

    Hope this helps.

  • Heya,

    I've got it working so far in an ASP.NET webform, but when the form is posted, the values in the textboxes linked to the date picker are empty strings. I assume this means they are not getting posted back. is there an easy way around this?

    - O8

  • @Orion, if you use ASP.NET TextBoxes, the ViewState should help you in that... Simply, copy the code in my example and add this:



    Then in code behind, paste this:

    protected void btnSend_Click(object sender, EventArgs e)
    {
    Response.Write("Hey..");
    }

    Once you click the button, the date values in the textboxes shouldn't be empty. If you do post to another page, than you will manage the state of the values by using some of the state management techniques.

    Hope this helps.

  • Hi

    Thanks for Nice article.

    I have a question here, by default I can see years in calendar control from 2001 to 2021, end user will be in a confusion whether he can select dates prior to 2001 or not. So is there anyway I can enable a scroll bar in year drop downlist, so can display years till 1950 or 1900 by default...

    Thanks in advance !!

    MOhan

  • Hi

    Thanks for u r Article its very useful for me for navigating in years

  • Hi can i need this with having an image button near textbox and by clicking on that i would like popup the calendar instead of popping up on focus of textbox can any one give me for this

  • Hi Dora,

    Add your calendar image (here is one: http://keith-wood.name/img/calendar.gif) and add the following HTML code:




    Then, on the text box where you want to create datepicker feature together with the calendar image, add this line of code:

    $('#startdate').datepick({showOnFocus: false, showTrigger: '#dpImg'});

    where 'dpImg' is the id of the img tag.

    Hope this helps.

  • hi hajan, nice code
    only one thing i found out is it doesn't work under master page. can u show a simple code that works for master page?

  • Hey jac,

    Try adding startdate and enddate selectors like this:
    $("#").datepick({ dateFormat: 'dd/mm/yyyy' });
    $("#").datepick({ dateFormat: 'dd/mm/yyyy' });

    This should make it work properly when your date-pickers are in pages that use Master pages.

    Hope this helps.

  • hi hajan,

    thanks for code, it works! Btw, can u show how to disable the select date before today, and also disable the dropdrown for month and year?

  • Hi jac,
    To disable month/year selection and set the min date to today (disable all dates before today), use the following code:

    $(function () {
    $('#').datepick({
    dateFormat: 'mm/dd/yyyy',
    minDate: 'today',
    changeMonth: false
    });
    $('#').datepick({
    dateFormat: 'dd/mm/yyyy'
    });
    });


    Hope this helps.

  • @Mark, thanks for the comment and for sharing the link. Well, jQuery UI DatePicker is also very good DP plugin. Afaik, the one I have shown in this blog post is used by the jQuery UI team for creating the jQuery UI Datepicker.

  • Thanks!
    Your article helped me a lot.

    When integrating this control in asp.net updatepanel, I found one problem ie the date gets reset to empty string. But after goin in details this turned out to be asp.net state mgmt problem. I was using ReadOnly property of the textbox (to stop direct user input).

    Then, adding attribute at Page_Load solved the issue e.g:

    txtRegDate.Attributes.Add("readonly", "readonly");

  • Nice article Hajan. In my website I am using textbox1 as selector for datepicker but on my C# side (asp.net) I am having hard time getting textbox1.text. Please provide your help.

  • Hi charan, can you tell me what's the exact problem? I would be glad to help you resolve it... On the other hand, you can also post your questions to asp.net forums at http://forums.asp.net

  • Hi,

    How do I bind the DatePicker to an EntityDataSource column so that the date is selected from the database and updated back to the database? I have the DatePicker working, and it is beautiful, but it isn't talking to my database.

    Thanks!

  • I found the answer to my question. &nbsp;All I had to do was bind the column to the TextBox control using the text property. &nbsp;Works like a charm!

    text='&lt;%#Bind("MovementDate", "{0:d}") %&gt;'

  • in master page its not working

  • Can any one help me how 2 add only timepicker plugin in mvc3 .cshtml and what steps do we need to follow up while configuring it.

  • YOU DID NOT POST JQUERY DATEPICKER SCRIPT.

  • Hi,

    $('#startdate').datepick({ minDate:'07/01/2010', maxDate:'07/15/2010' });

    this statement is not working.

    I also need help for:

    validation on DateTo so that it could not be less than DateFrom

  • Hm, I wonder what I should learn from this article... At least you should have added a SAVE-Button to show how to access values selected in the picker....?

  • Great!!! thanks for this

Comments have been disabled for this content.