Click here to Skip to main content
15,882,152 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 588.8K   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

 
GeneralRe: Control Placement Pin
Dan Colasanti16-Oct-04 22:07
professionalDan Colasanti16-Oct-04 22:07 
GeneralProblem with DatePicker Inside DataGrid as Template Column Pin
Member 4566512-Apr-04 2:12
Member 4566512-Apr-04 2:12 
GeneralRe: Problem with DatePicker Inside DataGrid as Template Column Pin
Doug Wilson14-Apr-04 20:31
Doug Wilson14-Apr-04 20:31 
GeneralRe: Problem with DatePicker Inside DataGrid as Template Column Pin
Dan Colasanti16-Oct-04 22:07
professionalDan Colasanti16-Oct-04 22:07 
Questionsamples? Pin
Huisheng Chen9-Apr-04 2:46
Huisheng Chen9-Apr-04 2:46 
AnswerRe: samples? Pin
Doug Wilson14-Apr-04 20:36
Doug Wilson14-Apr-04 20:36 
GeneralNetscape Pin
Fil_Bell8-Apr-04 0:11
Fil_Bell8-Apr-04 0:11 
GeneralRe: Netscape Pin
Doug Wilson8-Apr-04 1:35
Doug Wilson8-Apr-04 1:35 
GeneralRe: Netscape Pin
jmg578521-Jul-04 3:28
jmg578521-Jul-04 3:28 
GeneralMultiple javascript, styleclass and validator Pin
Danny Alvares7-Apr-04 0:28
Danny Alvares7-Apr-04 0:28 
GeneralRe: Multiple javascript, styleclass and validator Pin
Doug Wilson7-Apr-04 20:37
Doug Wilson7-Apr-04 20:37 
GeneralRe: Multiple javascript, styleclass and validator Pin
Danny Alvares19-Apr-04 12:35
Danny Alvares19-Apr-04 12:35 
GeneralRe: Multiple javascript, styleclass and validator Pin
Danny Alvares1-Oct-04 9:57
Danny Alvares1-Oct-04 9:57 
GeneralRe: Multiple javascript, styleclass and validator Pin
Doug Wilson3-Oct-04 20:18
Doug Wilson3-Oct-04 20:18 
QuestionDoes this work in C#? Pin
Shannon J Hager24-Feb-04 14:50
Shannon J Hager24-Feb-04 14:50 
AnswerRe: Does this work in C#? Pin
Doug Wilson25-Feb-04 19:14
Doug Wilson25-Feb-04 19:14 
AnswerRe: Does this work in C#? Pin
Doug Wilson25-Feb-04 19:14
Doug Wilson25-Feb-04 19:14 
Generalcalendar Pin
sat_thu_da_tinh21-Feb-04 14:14
sat_thu_da_tinh21-Feb-04 14:14 
GeneralRe: calendar Pin
Doug Wilson22-Feb-04 20:01
Doug Wilson22-Feb-04 20:01 
Generalcalendar Pin
sat_thu_da_tinh21-Feb-04 14:13
sat_thu_da_tinh21-Feb-04 14:13 
Generalproblems using DNN Modules Pin
Jonathan Scott4-Feb-04 21:38
Jonathan Scott4-Feb-04 21:38 
GeneralProblem using with C# Pin
Tom Wood30-Jan-04 22:03
Tom Wood30-Jan-04 22:03 
GeneralRe: Problem using with C# Pin
sides_dale1-Feb-04 16:50
sides_dale1-Feb-04 16:50 
GeneralRe: Problem using with C# Pin
aonachbeag8-Mar-04 3:26
aonachbeag8-Mar-04 3:26 
GeneralRe: Problem using with C# Pin
aonachbeag10-Mar-04 5:02
aonachbeag10-Mar-04 5:02 

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.