Click here to Skip to main content
Click here to Skip to main content

Ready to Use DatePicker Custom Control

By , 22 Sep 2006
Rate this:
Please Sign up or sign in to vote.

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:
  • <%@ Register TagPrefix="DP" NameSpace="MyOwnWebControls" Assembly="MyOwnWebControls" %>
  • Create an object in your aspx or ascx file, like this:
  • <DP:MyOwnWebControls.DatePickerControl id="DP_whatever_date" 
       Width="100px" RelPath="../" runat="server">
    </DP:MyOwnWebControls.DatePickerControl>
  • Create a server side variable in the code-behind:
  • Protected DP_whatever_date As MyOwnWebControls.MyOwnWebControls.DatePickerControl

Description of properties

The Custom Control supports these properties:

Property Type Defaultvalue Description
Text String   The content of the DatePicker edit field.
RelPath String ../ Relative path to the locations of the DatePicker images, beginning from the page with the DatePicker object.
Tooltip String   A tooltip.
Width String 64px Width in pixels.
CheckFunc String CheckDatePickerDate The 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.
Disabled Boolean False Decides 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:

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:

Parameter Type Defaultvalue Description
Page ByRef System.Web.UI.Page   Page where the DatePicker is.
oSession ByRef As System.Web.SessionState. HttpSessionState   Active Session object.
id String   The ID of the object.
value String   Active date.
relpath String ../ Relative path to the locations of the DatePicker images beginning from the page with the DatePicker object.
tooltip Optional String   The tooltip.
width Optional String 64px Width in pixels.
bVisible Optional Boolean True Visibility.
bDisabled Optional Boolean False Decided if it's a read-only control.
checkfunc Optional String CheckDatePickerDate The 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)

About the Author

Stephan Pilz
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 PinmemberLloyd Gregory16-Aug-07 8:19 
GeneralWrong date Pinmemberrtomaska24-Apr-07 4:41 
GeneralReturn mm.dd.yyyy PinmemberPatrick7572-Nov-06 20:22 
AnswerRe: Return mm.dd.yyyy PinmemberStephan Pilz6-Nov-06 22:34 
GeneralReturn mm.dd.yyyy PinmemberPatrick7572-Nov-06 20:22 
QuestionQuestions PinmemberMisty_Blue11-Oct-06 4:25 
QuestionRe: Questions PinmemberMisty_Blue11-Oct-06 4:45 
AnswerRe: Questions PinmemberStephan Pilz11-Oct-06 4:46 
GeneralVS 2005 web application Pinmemberyososndr4-Oct-06 3:57 
GeneralRe: VS 2005 web application PinmemberStephan Pilz4-Oct-06 23:57 
GeneralRe: VS 2005 web application PinmemberMisty_Blue11-Oct-06 4:23 
GeneralView State on disabled datepicker not working Pinmemberjvetterli1-Oct-06 18:54 
GeneralRe: View State on disabled datepicker not working PinmemberStephan Pilz5-Oct-06 0:03 
QuestionZ-Index Pinmemberjvetterli26-Sep-06 10:08 
AnswerRe: Z-Index PinmemberStephan Pilz26-Sep-06 20:52 
AnswerRe: Z-Index PinmemberStephan Pilz26-Sep-06 21:55 
GeneralRe: Z-Index Pinmemberjvetterli27-Sep-06 10:04 
Generaltypo error.. PinmemberRamu -15626-Sep-06 3:12 
GeneralRe: typo error.. PinmemberStephan Pilz26-Sep-06 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 22 Sep 2006
Article Copyright 2006 by Stephan Pilz
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid