Click here to Skip to main content
15,887,394 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 590.3K   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

 
QuestionRe: my 2 cents for the asp.net comunity Pin
Michael Freidgeim27-Nov-05 13:00
Michael Freidgeim27-Nov-05 13:00 
GeneralUsing your code! Pin
Pham Ngoc Quyen7-Nov-04 15:31
Pham Ngoc Quyen7-Nov-04 15:31 
GeneralSource Code Updates Pin
Dan Colasanti26-Oct-04 22:59
professionalDan Colasanti26-Oct-04 22:59 
GeneralSetting the TextBox Width Pin
Dan Colasanti15-Oct-04 10:48
professionalDan Colasanti15-Oct-04 10:48 
GeneralRe: Setting the TextBox Width Pin
Dan Colasanti16-Oct-04 12:33
professionalDan Colasanti16-Oct-04 12:33 
GeneralFix for Opera 7.X Compatibility Pin
Dan Colasanti15-Oct-04 10:39
professionalDan Colasanti15-Oct-04 10:39 
GeneralGot a tip: textbox and validator in one Pin
Danny Alvares4-Oct-04 16:28
Danny Alvares4-Oct-04 16:28 
GeneralRe: Got a tip: textbox and validator in one Pin
Dan Colasanti16-Oct-04 12:29
professionalDan Colasanti16-Oct-04 12:29 
I was able to use validators with the control by making it inherit from TextBox instead of from Control. This makes the control easier to use in many ways and simplifies the code too. For example, all of the m_text and m_css code (and the m_width code that I added in a previous post) becomes unnecessary since TextBox already has Text, CssClass, and Width properties. You can delete all of that code from the control.

Inheriting from TextBox also makes it easier to use the control with Validators - you'll also need to delete the line where the textbox ID is set to "foo" and delete the two _foo substrings. Note that you don't need to do this in order to make the control use validators (it already works with validators, but if you don't, you'll need to append :foo to the control's ID name in the validator, which is awkward). Once you change the inheritance to TextBox, you'll want to de-foo the source code.

Inheriting from TextBox has other useful effects:
(1) If you are using a "SetFocus" function (ie. from a custom BasePage) to set the focus to a control when the page is loaded or similarly use a SetDefaultButton function on your form, inheriting from TextBox makes the DatePicker compatible with these functions.
(2) It allows the control to be positioned in GridLayout mode, if desired, and to be put inside a DataGrid template column. People had mentioned in previous posts that they were unable to do these things with the original control.

Finally, you should remove the line that sets the textbox to ReadOnly. It can be changed using the inherited TextBox properties (or programmatically) if desired. Removing the ReadOnly will cause a usable textbox to display in Netscape, even though the calendar won't appear when it's clicked. This textbox can then be associated with Date validators, which will also work in Netscape.

Now your DatePicker can manipulate any of the Properties that a TextBox can, right from the Visual Studio .Net Properties page!

GeneralRe: Got a tip: textbox and validator in one Pin
doco au24-Oct-04 1:35
doco au24-Oct-04 1:35 
GeneralRe: Got a tip: textbox and validator in one Pin
jjborges21-May-07 8:28
jjborges21-May-07 8:28 
QuestionPlease Tell me why? Pin
Pham Ngoc Quyen20-Sep-04 23:09
Pham Ngoc Quyen20-Sep-04 23:09 
AnswerRe: Please Tell me why? Pin
Doug Wilson21-Sep-04 1:07
Doug Wilson21-Sep-04 1:07 
GeneralRe: Please Tell me why? Pin
Pham Ngoc Quyen21-Sep-04 8:05
Pham Ngoc Quyen21-Sep-04 8:05 
GeneralRe: Please Tell me why? Pin
Doug Wilson21-Sep-04 20:43
Doug Wilson21-Sep-04 20:43 
GeneralRe: Please Tell me why? Pin
Sbotros20-Dec-04 18:08
Sbotros20-Dec-04 18:08 
GeneralgetElementByID error Pin
kjtan14-Sep-04 19:32
kjtan14-Sep-04 19:32 
QuestionProblem???? Pin
bevans19752-Sep-04 10:51
bevans19752-Sep-04 10:51 
AnswerRe: Problem???? Pin
Doug Wilson2-Sep-04 20:50
Doug Wilson2-Sep-04 20:50 
GeneralRe: Problem???? Pin
Anonymous3-Sep-04 4:42
Anonymous3-Sep-04 4:42 
AnswerRe: Problem???? Pin
an_phu1-Sep-05 16:22
an_phu1-Sep-05 16:22 
GeneralImages are not working Pin
jmg578515-Jul-04 10:09
jmg578515-Jul-04 10:09 
GeneralRe: Images are not working Pin
RichBaker20-Jul-04 3:12
sussRichBaker20-Jul-04 3:12 
GeneralRe: Images are not working Pin
Kasie17-May-05 5:30
Kasie17-May-05 5:30 
GeneralThanks for the control! Pin
svm1724-May-04 12:11
svm1724-May-04 12:11 
GeneralRe: Thanks for the control! Pin
Doug Wilson25-May-04 1:58
Doug Wilson25-May-04 1:58 

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.