Click here to Skip to main content
Click here to Skip to main content

Web DatePicker Control

, 29 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Article on how I built my Date Picker Control.

Updates

Thanks to Camilo Orozco for providing me with the translated uncompiled C# code. The .cs file had been added to the source code zip. Sorry it's taken so long to post.

Introduction

One of the most common causes of application failure, especially for new developers, is handling date input from the user. If it's in the wrong format and you try to write it to the database, then the database command fails. If the user switches the day and month then your date could be wrong, even if you're using a regular expression validator (i.e. 01/02/2004 could be 2 January 2004 or 1 February 2004).

To get around this, I used to wire a server side calendar control to the textbox onClick event, and launch it in a new window, forcing the user to select a date, then return the date in the format of my choice to the textbox. But while this is all well and good for simple web apps, this week I needed the same functionality to be launched from inside a server component I was writing.

Background

I remember that in the old days of ASP, there was a lengthy JavaScript solution we used to use, that would be perfect for what I was trying to do now. I asked a few people, and a lot of guys sent me the same code back. Full credit must go to Tan Ling Wee for writing the original JavaScript code, even though I've never met him (or her - not quite sure), and to all the people who passed it on until it eventually arrived in my email box. Now here was a piece of code perfect for what I needed to do, but it's over 600 lines long, and there's no way in hell I was going to manually reformat it line by line so that I could render it to the client. So, as any really lazy developer does things, I wrote my own app to process the JavaScript and return a code block that I could simply copy and paste into Visual Studio (will write another article on this, and upload the app for use by all). After that, it was simply a matter of writing a new class that inherited from the control, added properties to make the control customizable, and compiled.

Using the code

If you're using Visual Studio, you can simply add the control to the Toolbox, and then drag and drop it into your apps. For everyone else, first copy the DLL to the bin directory of your project. Then, on your aspx page, you'll first need to register the component as follows:

<%@ Register TagPrefix="cc1" 
Namespace="DatePicker.iX.Controls"
    Assembly="DatePicker" %>

Then place the control on the page like so:

<cc1:DatePicker id="DatePicker1" runat="server"
  imgDirectory="/WebApplication6/img/" DateType="dd mmm yyyy">
</cc1:DatePicker>

You'll notice the properties imgDirectory and DateType. imgDirectory is the path to the directory where the control images needed can be found (images included in the download files). DateType is the format in which the date should be returned. "dd mmm yyyy" writes out the full date, e.g. "01 January 2003", but this can be changed to any format by changing this property (e.g. "dd/mm/yyyy" or "mm/dd/yyyy"). There is also a CssClass property, which sets the CssClass for the textbox, and a Text property, to set the start text to be displayed in the textbox.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Doug Wilson
Web Developer
South Africa South Africa
Doug is an Applications Integrator for an online gaming company. He has been programming for 9 years, and has been working with the .NET framework since the beginning of 2003, in both VB.NET & C#.

Comments and Discussions

 
GeneralMy vote of 1 PinmemberAubri22-Feb-11 7:59 
GeneralIt does not work at all Pinmemberrizwanaarif1-Feb-11 3:41 
GeneralLooked good - but.... PinmemberDunkol21-Sep-10 11:40 
Generaldoes not work in firefox,chrome PinmemberTHINK1231-Jun-10 0:41 
GeneralNot able to get the value in the control,plz help [modified] Pinmembervickyboina23-Apr-10 2:46 
Questioncan you create one that supports Hijri calendars? Pinmemberrashadkk1-Apr-10 1:01 
QuestionHow to get the date from the date picker control Pinmemberravi_27may15-Feb-10 22:42 
AnswerRe: How to get the date from the date picker control PinmemberHendrik Debedts12-Apr-10 1:35 
Generalyour datepicker doesn't work with ajax Pinmembersameer_s26-Aug-09 3:22 
QuestionHow to use calendar img beside the text control and populate the date on click of img ? PinmemberMember 208195111-Jul-09 7:38 
QuestionDefault date is set every time Pinmembernaughtygirl_sony8-Feb-09 23:02 
Questioncan't work inside an ajax updatepanel inside a master page Pinmembermausepul22-Jan-09 10:54 
GeneralMy vote of 1 Pinmemberwchvic16-Dec-08 18:37 
QuestionHow to get its value in .cs PinmemberTripathi Swati10-Sep-08 0:38 
AnswerRe: How to get its value in .cs Pinmemberp05esto19-May-09 8:53 
QuestionAnyone get the calendar working in IE7? Pinmemberpliant616-Jul-08 4:31 
AnswerFailure to Cast, and getting the selected date PinmemberJoe Petitti19-Jun-08 7:49 
GeneralRe: Failure to Cast, and getting the selected date Pinmembermonica00522-Sep-08 1:47 
GeneralGetting the value from the control PinmemberCodingJeff17-Jun-08 11:00 
GeneralCustomer Control Overwrite PinmemberMember 420460516-Jun-08 23:08 
GeneralFirst time user!! Pinmembercoolmesh844-Jun-08 1:18 
GeneralUnable to cast object of type ... PinmemberNickNnator20-Feb-08 8:48 
GeneralRe: Unable to cast object of type ... Pinmembermariops15-May-08 16:39 
GeneralRe: Unable to cast object of type ... Pinmemberwchvic16-Dec-08 16:29 
Questionhow will i assign values to the control ? Pinmembervaratha18-Feb-08 13:09 
AnswerRe: how will i assign values to the control ? Pinmembercheebeng2-Mar-08 17:16 
GeneralRe: how will i assign values to the control ? Pinmemberclov1-Jul-08 9:25 
QuestionCant get a handle on Text property Pinmembergmri.net20-Jul-07 11:50 
Im using a Multiview Form with this control. While it looks great, none of us over here can get a handle on the Text property of the control.
I am calling the control "bDate" - but I noticed through the DOM explorer (IE) that this control ID is rewritten as "bDate_foo". Still , I am unable to get the value from bDate_foo.Text. There are alot of other attempts as well, but for the sake of brevity I will stop here.
 
Been trying for hours, and in spite of everything I am trying the Text property returns as ""; (nothing)
 
Whats the trick???
AnswerRe: Cant get a handle on Text property PinmemberCodingJeff17-Jun-08 11:20 
Generalusing DatePicker control in DataGrid PinmemberKiran Shaikh22-Jun-07 8:06 
GeneralRe: using DatePicker control in DataGrid Pinmembertalf00820-Aug-07 10:32 
GeneralRe: using DatePicker control in DataGrid Pinmemberstixoffire21-May-08 23:27 
GeneralStill having issue with PostBack Pinmemberdowens318-Jun-07 5:21 
QuestionRe: Still having issue with PostBack PinmemberK.Senthil Kumar18-Nov-07 21:20 
AnswerRe: Still having issue with PostBack Pinmemberadhakhwa25-Aug-10 7:28 
Generaltrigger event PinmemberLadyReader30-May-07 6:46 
GeneralRequiredFieldValidator Pinmemberernest_elias13-Apr-07 4:45 
GeneralRe: RequiredFieldValidator Pinmemberstixoffire21-May-08 23:53 
Questionhow to make a link between textbox and monthcalendar in vb.net PinmemberSai Sankar26-Mar-07 22:08 
Questionerror while using the DLL Can any one help me in this regard Pinmemberhareendra5-Mar-07 23:08 
AnswerRe: error while using the DLL Can any one help me in this regard PinmemberMatt Saltz27-Mar-07 11:12 
GeneralRe: error while using the DLL Can any one help me in this regard Pinmemberctaeger28-Mar-07 6:05 
GeneralRe: error while using the DLL Can any one help me in this regard PinmemberMatt Saltz28-Mar-07 8:49 
GeneralRe: error while using the DLL Can any one help me in this regard Pinmemberctaeger28-Mar-07 10:22 
GeneralRe: error while using the DLL Can any one help me in this regard PinmemberNiN9E2-Aug-07 0:01 
GeneralGood Job PinmemberInggo28-Feb-07 19:57 
GeneralRe: Good Job PinmemberMatt Saltz27-Mar-07 12:11 
GeneralFinally i could solve the problem of casting problem and it works fine Pinmemberreal_harami25-Feb-07 7:51 
GeneralRe: Finally i could solve the problem of casting problem and it works fine Pinmemberreal_harami27-Feb-07 1:13 
GeneralRe: Finally i could solve the problem of casting problem and it works fine Pinmemberlukevin23-Apr-08 3:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 30 Mar 2004
Article Copyright 2004 by Doug Wilson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid