Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Ready to Use DatePicker Custom Control

3.00/5 (4 votes)
22 Sep 2006CPOL3 min read 1   650  
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)