Click here to Skip to main content
11,927,711 members (47,927 online)
Click here to Skip to main content
Add your own
alternative version


77 bookmarked

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.


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.


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.


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" 
    Assembly="DatePicker" %>

Then place the control on the page like so:

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

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.


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


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#.

You may also be interested in...

Comments and Discussions

GeneralRe: Finally i could solve the problem of casting problem and it works fine Pin
lukevin23-Apr-08 3:11
memberlukevin23-Apr-08 3:11 
GeneralHi I need to use the dll for Image control instead of Textbox Pin
somabss8-Jan-07 5:22
membersomabss8-Jan-07 5:22 
GeneralImproving Javascript rendering Pin
KenTheMan28-Dec-06 19:04
memberKenTheMan28-Dec-06 19:04 
GeneralRe: Improving Javascript rendering Pin
stixoffire21-May-08 23:43
memberstixoffire21-May-08 23:43 
GeneralProblem with postback value Pin
Adrian P. Eidelman20-Dec-06 15:15
memberAdrian P. Eidelman20-Dec-06 15:15 
AnswerRe: Problem with postback value Pin
Adrian P. Eidelman21-Dec-06 7:26
memberAdrian P. Eidelman21-Dec-06 7:26 
Questionis the code free for commercial use? Pin
zhuojing xie19-Nov-06 17:10
memberzhuojing xie19-Nov-06 17:10 
AnswerRe: is the code free for commercial use? Pin
Doug Wilson20-Nov-06 10:19
memberDoug Wilson20-Nov-06 10:19 
Generaldll Pin
captain121031-Oct-06 5:47
membercaptain121031-Oct-06 5:47 
GeneralRe: dll Pin
Jon Hermiz15-Aug-07 10:57
memberJon Hermiz15-Aug-07 10:57 
QuestionDoes this work on Firefox? Pin
Walrez30-Oct-06 1:30
memberWalrez30-Oct-06 1:30 
QuestionI don't view images for my date picker Pin
Misty_Blue11-Oct-06 17:45
memberMisty_Blue11-Oct-06 17:45 
AnswerRe: I don't view images for my date picker Pin
Matt Saltz27-Mar-07 10:47
memberMatt Saltz27-Mar-07 10:47 
GeneralRe: I don't view images for my date picker Pin
stixoffire21-May-08 23:39
memberstixoffire21-May-08 23:39 
General528 string concatenations (in "placeJavascript" method) Pin
MarcTo21-Sep-06 10:24
memberMarcTo21-Sep-06 10:24 
GeneralAdding date picker Pin
nisharmca18-Sep-06 20:19
membernisharmca18-Sep-06 20:19 
GeneralRe: Adding date picker Pin
Ricaj06254-Oct-06 9:07
memberRicaj06254-Oct-06 9:07 
QuestionUsing two DatePickers on the same page Pin
sympthom 918-Sep-06 0:18
membersympthom 918-Sep-06 0:18 
AnswerRe: Using two DatePickers on the same page Pin
Matt Saltz27-Mar-07 12:08
memberMatt Saltz27-Mar-07 12:08 
Questiondefault date Pin
Tim Vandeweerd28-Jul-06 14:26
memberTim Vandeweerd28-Jul-06 14:26 
AnswerRe: default date Pin
Matt Saltz27-Mar-07 11:06
memberMatt Saltz27-Mar-07 11:06 
GeneralProblems of compatibility with Busyboxdotnet Pin
YaKs6924-Jul-06 6:29
memberYaKs6924-Jul-06 6:29 
General'Undifined' is null or not a object Pin
Atul Mohan19-Jul-06 4:32
memberAtul Mohan19-Jul-06 4:32 
GeneralTextBox Pin
8712510420-Jun-06 0:53
member8712510420-Jun-06 0:53 
GeneralRe: TextBox Pin
Thang Pham29-Jun-06 22:22
memberThang Pham29-Jun-06 22:22 
Questionhow can i set this type date Pin
Parag Chitodkar14-Jun-06 21:40
memberParag Chitodkar14-Jun-06 21:40 
GeneralType Casting Error Pin
galburt15-Apr-06 22:42
membergalburt15-Apr-06 22:42 
GeneralRe: Type Casting Error [modified] Pin
descon10-Oct-06 20:52
memberdescon10-Oct-06 20:52 
GeneralRe: Type Casting Error Pin
Habib Alvi6-Nov-06 13:05
memberHabib Alvi6-Nov-06 13:05 
GeneralRe: Type Casting Error Pin
rpallas21-Nov-06 15:05
memberrpallas21-Nov-06 15:05 
GeneralRe: Type Casting Error Pin
Mark Phalen22-Nov-06 9:26
memberMark Phalen22-Nov-06 9:26 
GeneralRe: Type Casting Error Pin
rpallas22-Nov-06 15:07
memberrpallas22-Nov-06 15:07 
GeneralRe: Type Casting Error Pin
KenTheMan28-Dec-06 17:38
memberKenTheMan28-Dec-06 17:38 
GeneralRe: Type Casting Error Pin
ggomez0395-Jan-07 11:02
memberggomez0395-Jan-07 11:02 
GeneralThe type or namespace name 'iX' does not exist in the class or namespace 'DatePicker.iX.Controls.DatePicker' (are you missing an assembly reference?) Pin
smilingsatan7-Apr-06 10:02
membersmilingsatan7-Apr-06 10:02 
GeneralRe: The type or namespace name 'iX' does not exist in the class or namespace 'DatePicker.iX.Controls.DatePicker' (are you missing an assembly reference?) Pin
DracoL2-May-06 0:10
memberDracoL2-May-06 0:10 
GeneralTrouble reading the date value in GridView Edit Template Pin
pac908153-Apr-06 18:03
memberpac908153-Apr-06 18:03 
AnswerRe: Trouble reading the date value in GridView Edit Template Pin
laxmiprashanth28-Aug-06 2:55
memberlaxmiprashanth28-Aug-06 2:55 
QuestionProblem with combobox Pin
YaKs6930-Mar-06 4:57
memberYaKs6930-Mar-06 4:57 
AnswerRe: Problem with combobox Pin
stixoffire22-May-08 0:00
memberstixoffire22-May-08 0:00 
GeneralNew Version Pin
IWantToDownload17-Mar-06 11:03
memberIWantToDownload17-Mar-06 11:03 
Question"Cutting" page problem Pin
yanek615-Mar-06 4:46
memberyanek615-Mar-06 4:46 
GeneralCode Correction Pin
sikemullivan18-Feb-06 16:25
membersikemullivan18-Feb-06 16:25 
GeneralRe: Code Correction Pin
bugmenot12324-Feb-06 5:05
memberbugmenot12324-Feb-06 5:05 
GeneralRe: Code Correction Pin
ccf_lau3-Mar-06 15:46
memberccf_lau3-Mar-06 15:46 
GeneralRe: Code Correction Pin
sikemullivan4-Mar-06 11:17
membersikemullivan4-Mar-06 11:17 
GeneralRe: Code Correction Pin
mingssn22-Aug-06 11:37
membermingssn22-Aug-06 11:37 
QuestionNot Working in .Net 2.0 using Master Pages Pin
cran2k7-Feb-06 21:54
membercran2k7-Feb-06 21:54 
AnswerRe: Not Working in .Net 2.0 using Master Pages Pin
Doug Wilson8-Feb-06 1:17
memberDoug Wilson8-Feb-06 1:17 
GeneralRe: Not Working in .Net 2.0 using Master Pages Pin
cran2k8-Feb-06 3:53
membercran2k8-Feb-06 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.151126.1 | Last Updated 30 Mar 2004
Article Copyright 2004 by Doug Wilson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid