Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual Basic
Article

Web DatePicker Control

Rate me:
Please Sign up or sign in to vote.
4.56/5 (49 votes)
29 Mar 20043 min read 586.1K   5.1K   76   229
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:

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

Then place the control on the page like so:

ASP.NET
<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


Written By
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

 
QuestionDatePicker Control Pin
SegunOk25-Oct-15 16:10
SegunOk25-Oct-15 16:10 
GeneralMy vote of 1 Pin
Aubri22-Feb-11 6:59
Aubri22-Feb-11 6:59 
GeneralIt does not work at all Pin
Rizwan Aarif1-Feb-11 2:41
Rizwan Aarif1-Feb-11 2:41 
GeneralLooked good - but.... Pin
Dunkol21-Sep-10 10:40
Dunkol21-Sep-10 10:40 
Generaldoes not work in firefox,chrome Pin
THINK12331-May-10 23:41
THINK12331-May-10 23:41 
GeneralNot able to get the value in the control,plz help [modified] Pin
vickyboina23-Apr-10 1:46
vickyboina23-Apr-10 1:46 
Questioncan you create one that supports Hijri calendars? Pin
rashadkk1-Apr-10 0:01
rashadkk1-Apr-10 0:01 
QuestionHow to get the date from the date picker control Pin
ravi_27may15-Feb-10 21:42
ravi_27may15-Feb-10 21:42 
AnswerRe: How to get the date from the date picker control Pin
Hendrik Debedts12-Apr-10 0:35
Hendrik Debedts12-Apr-10 0:35 
Is there already some solution to this problem? I can 't find one
Generalyour datepicker doesn't work with ajax Pin
Sam_IN26-Aug-09 2:22
Sam_IN26-Aug-09 2:22 
QuestionHow to use calendar img beside the text control and populate the date on click of img ? Pin
Member 208195111-Jul-09 6:38
Member 208195111-Jul-09 6:38 
QuestionDefault date is set every time Pin
naughtygirl_sony8-Feb-09 22:02
naughtygirl_sony8-Feb-09 22:02 
Questioncan't work inside an ajax updatepanel inside a master page Pin
mausepul22-Jan-09 9:54
mausepul22-Jan-09 9:54 
GeneralMy vote of 1 Pin
wchvic16-Dec-08 17:37
wchvic16-Dec-08 17:37 
QuestionHow to get its value in .cs Pin
Tripathi Swati9-Sep-08 23:38
Tripathi Swati9-Sep-08 23:38 
AnswerRe: How to get its value in .cs Pin
p05esto19-May-09 7:53
p05esto19-May-09 7:53 
QuestionAnyone get the calendar working in IE7? Pin
pliant616-Jul-08 3:31
pliant616-Jul-08 3:31 
AnswerFailure to Cast, and getting the selected date Pin
Joe Petitti19-Jun-08 6:49
Joe Petitti19-Jun-08 6:49 
GeneralRe: Failure to Cast, and getting the selected date Pin
monica00522-Sep-08 0:47
monica00522-Sep-08 0:47 
GeneralGetting the value from the control Pin
CodingJeff17-Jun-08 10:00
CodingJeff17-Jun-08 10:00 
GeneralCustomer Control Overwrite Pin
Member 420460516-Jun-08 22:08
Member 420460516-Jun-08 22:08 
GeneralFirst time user!! Pin
coolmesh844-Jun-08 0:18
coolmesh844-Jun-08 0:18 
GeneralUnable to cast object of type ... Pin
NickNnator20-Feb-08 7:48
NickNnator20-Feb-08 7:48 
GeneralRe: Unable to cast object of type ... Pin
mariops15-May-08 15:39
mariops15-May-08 15:39 
GeneralRe: Unable to cast object of type ... Pin
wchvic16-Dec-08 15:29
wchvic16-Dec-08 15:29 

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.