Click here to Skip to main content
6,630,586 members and growing! (17,633 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Web DatePicker Control

By Doug Wilson

Article on how I built my Date Picker Control.
VB, Windows, .NET, Visual Studio, Dev
Posted:18 Jan 2004
Updated:29 Mar 2004
Views:270,367
Bookmarked:74 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
47 votes for this article.
Popularity: 6.74 Rating: 4.03 out of 5
2 votes, 4.3%
1
1 vote, 2.1%
2
4 votes, 8.5%
3
14 votes, 29.8%
4
26 votes, 55.3%
5

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

About the Author

Doug Wilson


Member
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#.
Occupation: Web Developer
Location: South Africa South Africa

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 219 (Total in Forum: 219) (Refresh)FirstPrevNext
Generalyour datepicker doesn't work with ajax Pinmembersameer_s3:22 26 Aug '09  
GeneralHow to use calendar img beside the text control and populate the date on click of img ? PinmemberMember 20819517:38 11 Jul '09  
QuestionDefault date is set every time Pinmembernaughtygirl_sony23:02 8 Feb '09  
Questioncan't work inside an ajax updatepanel inside a master page Pinmembermausepul10:54 22 Jan '09  
GeneralMy vote of 1 Pinmemberwchvic18:37 16 Dec '08  
GeneralHow to get its value in .cs PinmemberTripathi Swati0:38 10 Sep '08  
GeneralRe: How to get its value in .cs Pinmemberp05esto8:53 19 May '09  
GeneralAnyone get the calendar working in IE7? Pinmemberpliant64:31 16 Jul '08  
AnswerFailure to Cast, and getting the selected date PinmemberJoe Petitti7:49 19 Jun '08  
GeneralRe: Failure to Cast, and getting the selected date Pinmembermonica0051:47 22 Sep '08  
GeneralGetting the value from the control PinmemberCodingJeff11:00 17 Jun '08  
GeneralCustomer Control Overwrite PinmemberMember 420460523:08 16 Jun '08  
GeneralFirst time user!! Pinmembercoolmesh841:18 4 Jun '08  
GeneralUnable to cast object of type ... PinmemberNickNnator8:48 20 Feb '08  
GeneralRe: Unable to cast object of type ... Pinmembermariops16:39 15 May '08  
GeneralRe: Unable to cast object of type ... Pinmemberwchvic16:29 16 Dec '08  
Questionhow will i assign values to the control ? Pinmembervaratha13:09 18 Feb '08  
AnswerRe: how will i assign values to the control ? Pinmembercheebeng17:16 2 Mar '08  
GeneralRe: how will i assign values to the control ? Pinmemberclov9:25 1 Jul '08  
QuestionCant get a handle on Text property Pinmembergmri.net11:50 20 Jul '07  
AnswerRe: Cant get a handle on Text property PinmemberCodingJeff11:20 17 Jun '08  
Generalusing DatePicker control in DataGrid PinmemberKiran Shaikh8:06 22 Jun '07  
GeneralRe: using DatePicker control in DataGrid Pinmembertalf00810:32 20 Aug '07  
GeneralRe: using DatePicker control in DataGrid Pinmemberstixoffire23:27 21 May '08  
GeneralStill having issue with PostBack Pinmemberdowens35:21 18 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Mar 2004
Editor: Nishant Sivakumar
Copyright 2004 by Doug Wilson
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project