web 2.0

MVC - Using the jQuery DatePicker

jQuery is the greatest thing since sliced bread. The amount of functionality built into those javascript libraries is absolutely amazing (Thanks to John Resig!). Today, while I was working on my company website I discovered that I really need some sort of datepicker for one of my admin pages. Luckily, the whole process of adding a datepicker only takes seconds with the jQuery UI libraries.

  1. Visit the jQuery UI download page. On the right hand side of the page you will see a download button. However, before you hit that button you may want to select a theme first. For my demo I chose black-tie because it matches the color scheme of my site. Also, notice that you can customize the download to include/exclude functionality. This is good if you want to reduce the size of the javascript file to make browsing faster for your users. Anyway, once you finish your customization (for the sake of this demo, make sure you do not uncheck the Datepicker) click the Download button.
  2. Add the script and optional style sheet references to your webpage. Mine looks like this, but you may need to customize yours to match your directory structure:
  3. <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="/Scripts/jquery-ui-1.7.2.custom.css" />
  4. Add the HTML code to your page. You have two options here..
    • Without Using HtmlHelper:
      <script type="text/javascript"> 
         $(document).ready(function(){ 
            $("#FromDate").datepicker(); 
         }); 
      </script> 
      <input type="text" id="FromDate" name="FromDate" value=""/> 
    • With HtmlHelper: (recommended method because it will clean up your source code and lend itself to reusability)
      Create a new class in your MVC project.
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
       
      namespace System.Web.Mvc.Html {
          public static class DatePickerExtension {
              public static string DatePicker(this HtmlHelper htmlHelper, string name, string value )
              {
                  return "<script type=\"text/javascript\">" +
                           "$(function() {" +
                           "$(\"#" + name + "\").datepicker();" +
                           "});" +
                           "</script>" +
                           "<input type=\"text\" size=\"10\" value=\"" + value + "\" id=\"" + name + "\" name=\"" + name + "\"/>";             
              }
          }
      }
      Now you can use the extension method you created to insert the html code and script...
      <%= Html.DatePicker( "Date", Html.Encode(String.Format("{0:MM/dd/yyyy}", Model.Date)) ) %> 

And here are the results...

Additional Resources:

Comments

Robje , on 12/17/2009 2:29:10 PM Said:

Robje

I am not familiar with MVC (not yet). So I don't know the (possible) advantages of it. But I was looking for using the jQuery datepicker on my ASP.NET site. I found another way, easier as far as I can see now. See blog.vanmeeuwen-online.nl/.../...atepicker-on.html

Michael Ceranski , on 12/17/2009 2:29:10 PM Said:

Michael Ceranski

Actually I think the helper should set the name and the ID.

Name is key in addition to using Form Elements: http://www.w3.org/TR/html401/interact/forms.html.

Michael Ceranski , on 12/17/2009 2:29:10 PM Said:

Michael Ceranski

In order for the HtmlHelper code to work properly with MVC and JQuery you need to use id and name. It seems that JQuery functions that reference elements by using this syntax: $("#someHTMLElement") rely on the id value. Whereas ASP.NET relies on the name attribute for binding. That's why I chose to set both values in the HtmlHelper.

Michael Ceranski , on 12/17/2009 2:29:10 PM Said:

Michael Ceranski

I just updated it to include the ID and the name. At some point I discovered that I should have used both but I forgot to go back and update the article. Thanks for the feedback.

Monty Love United States, on 1/10/2010 6:17:03 PM Said:

Monty Love

I am trying to use datepicker in a modal dialog and I can get the calendar to display if I use a <Div> tag with an ID="datapicker", but it won't do anything, plus it is being displayed in-line.

If I set the field ID="datapicker" I can see the button with the calendar gif, but it does not display the calendar.  

Any ideas what is going on here?

Thanks,
Monty

Comments are closed