Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET

Ready to Use DatePicker Custom Control

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
22 Sep 2006CPOL3 min read 63.3K   650   32   19
DatePicker WebControl based on the DatePicker of Guyon A. Roche.

Sample Image - DatePickerCustomControl.jpg

Introduction

The article of Nikita D. Sinelnikoff and the source code of Guyon A. Roche inspired me to encapsulate the DatePicker inside a .NET Custom Control.

In addition, the source code of Nikita contained some mistakes, and I still had some useful extension ideas.

Bug fixes on Nikita's code and extensions

  • If I click on the "Next Week" button, there is a missing object in JavaScript. I found that the function getDaysInMonth doesn't exist.
  • Problem of dynamic positioning of the span tag.
  • I extended the code with some nice colors. In this connection, I think it's a good idea to highlight the actual date.
  • I added a check function on the input-field that checks the date for correct input if the user enters a date manually.
  • I extended the class for multi-language support.
  • I encapsulated all the functions in a custom control written in VB.NET.

How to use

There are the steps to use the control:

  • Modify the DatePickerDynObject.GetLabels method if you want multi-language support. I can not post here the translation component I use in the real product environment!!! Maybe my article to generate multi-language websites very easily can help you.
  • Copy MyOwnWebControls.dll in your solution bin folder and/or create a .NET Reference.
  • Copy the Images and Js folders to your project folder.
  • In your aspx or ascx file, append a line to publish the control, like this:
  • ASP.NET
    <%@ Register TagPrefix="DP" NameSpace="MyOwnWebControls" Assembly="MyOwnWebControls" %>
  • Create an object in your aspx or ascx file, like this:
  • ASP.NET
    <DP:MyOwnWebControls.DatePickerControl id="DP_whatever_date" 
       Width="100px" RelPath="../" runat="server">
    </DP:MyOwnWebControls.DatePickerControl>
  • Create a server side variable in the code-behind:
  • VB
    Protected DP_whatever_date As MyOwnWebControls.MyOwnWebControls.DatePickerControl

Description of properties

The Custom Control supports these properties:

PropertyTypeDefaultvalueDescription
TextString The content of the DatePicker edit field.
RelPathString../Relative path to the locations of the DatePicker images, beginning from the page with the DatePicker object.
TooltipString A tooltip.
WidthString64pxWidth in pixels.
CheckFuncStringCheckDatePickerDateThe JS function to check the user input. You can write your own CheckMethod for specialized checks. The JS function must have only one parameter (the object ID) and returns true/false.
DisabledBooleanFalseDecides if it's a read-only control

Special use mode

In my company, we often use the code-behind to generate some HTML objects with static HTML text and put this text inside a PlaceHolder with a LiteralControl. The DatePicker supports this feature too. It means, you can use the internal DatePickerDynObject class and call the GenerateDatePicker method to produce the core HTML stream used for the LiteralControl. Here comes some example code:

VB
Dim oDP As New TXTChainWebControls.DatePickerDynObject
Dim oLiteral as LiteralControl

oLiteral = new LiteralControl (oDP.GenerateDatePicker(Me, Session, _
           "DP_whatever_date", "24.12.2006", "../"))

This method supports the following parameters:

ParameterTypeDefaultvalueDescription
PageByRef System.Web.UI.Page Page where the DatePicker is.
oSessionByRef As System.Web.SessionState. HttpSessionState Active Session object.
idString The ID of the object.
valueString Active date.
relpathString../Relative path to the locations of the DatePicker images beginning from the page with the DatePicker object.
tooltipOptional String The tooltip.
widthOptional String64pxWidth in pixels.
bVisibleOptional BooleanTrueVisibility.
bDisabledOptional BooleanFalseDecided if it's a read-only control.
checkfuncOptional StringCheckDatePickerDateThe JS function to check the user input. You can write your own CheckMethod for specialized checks. The JS function must have only one parameter (the object ID), and returns true/false.

History

  • 27.09.2006 - Bug fix.
    • Solved the overlay problem with static listboxes (no z-order support for dynamically created listboxes).
  • 22.09.2006 - Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
I started 1987 when I was about 14 years old, with Basic and Pascal on 8Bit Atari with insanely 130KB of memory, later 16Bit Atari (4Megs). In study I learn C, C++, Modula-2, Assembler and other science languages (Prolog, ADA). I hold a german engineer's degree Dipl.-Ing. (FH) in "Technical Computer Science" from the University of Applied Sciences Mittweida, Germany (comparable with a Master Degree).

Now I develop PPS and Supply-Chain-Software for middle and great companies in Chemnitz/Germany with Web- and MFC-Frontends in C, C++, ASP.NET, ADO.NET, XML, HTML, JavaScript.

My main tasks are specify and write down solutions of problems and implement the first code snippets.

In my spare time I like to drive a (fast) motocycle, read many science fiction and fantasy books and I'm a enthusiastically shotokan karate fighter. In winter I falling down from hill with alpin ski on foots, because snowboards are only for drug sniffers Wink | ;-)

Comments and Discussions

 
QuestionDesignTime View Pin
Lloyd Gregory16-Aug-07 8:19
Lloyd Gregory16-Aug-07 8:19 
GeneralWrong date Pin
rtomaska24-Apr-07 4:41
rtomaska24-Apr-07 4:41 
GeneralReturn mm.dd.yyyy Pin
Patrick7572-Nov-06 20:22
Patrick7572-Nov-06 20:22 
AnswerRe: Return mm.dd.yyyy Pin
Stephan Pilz6-Nov-06 22:34
Stephan Pilz6-Nov-06 22:34 
GeneralReturn mm.dd.yyyy Pin
Patrick7572-Nov-06 20:22
Patrick7572-Nov-06 20:22 
QuestionQuestions Pin
Misty_Blue11-Oct-06 4:25
Misty_Blue11-Oct-06 4:25 
QuestionRe: Questions Pin
Misty_Blue11-Oct-06 4:45
Misty_Blue11-Oct-06 4:45 
AnswerRe: Questions Pin
Stephan Pilz11-Oct-06 4:46
Stephan Pilz11-Oct-06 4:46 
GeneralVS 2005 web application Pin
yososndr4-Oct-06 3:57
yososndr4-Oct-06 3:57 
GeneralRe: VS 2005 web application Pin
Stephan Pilz4-Oct-06 23:57
Stephan Pilz4-Oct-06 23:57 
Sorry Yosef,

no idea. It's not possible for me to test the control with VS2005. It's developed with VS2003 und .NET1.1

BR
Stephan

                   \\\|///
                 \\  - -  //
                  (  @ @  )
+---------------oOOo-(_)-oOOo-----------------+
| Stephan Pilz   stephan.pilz@stephan-pilz.de |
|                <a href=http://www.stephan-pilz.de>www.stephan-pilz.de</a>          |
|                ICQ#: 127823481              |
+-----------------------Oooo------------------+
                 oooO   (   )
                (   )    ) /
                 \ (    (_/
                  \_)

GeneralRe: VS 2005 web application Pin
Misty_Blue11-Oct-06 4:23
Misty_Blue11-Oct-06 4:23 
GeneralView State on disabled datepicker not working Pin
jvetterli1-Oct-06 18:54
jvetterli1-Oct-06 18:54 
GeneralRe: View State on disabled datepicker not working Pin
Stephan Pilz5-Oct-06 0:03
Stephan Pilz5-Oct-06 0:03 
QuestionZ-Index Pin
jvetterli26-Sep-06 10:08
jvetterli26-Sep-06 10:08 
AnswerRe: Z-Index Pin
Stephan Pilz26-Sep-06 20:52
Stephan Pilz26-Sep-06 20:52 
AnswerRe: Z-Index Pin
Stephan Pilz26-Sep-06 21:55
Stephan Pilz26-Sep-06 21:55 
GeneralRe: Z-Index Pin
jvetterli27-Sep-06 10:04
jvetterli27-Sep-06 10:04 
Generaltypo error.. Pin
Ram -15626-Sep-06 3:12
Ram -15626-Sep-06 3:12 
GeneralRe: typo error.. Pin
Stephan Pilz26-Sep-06 4:04
Stephan Pilz26-Sep-06 4:04 

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.