Click here to Skip to main content
10,500,000 members (86,546 online)
Click here to Skip to main content

A Simple Date Selector User Control in ASP.NET (JavaScript based)

, 10 Mar 2005 478.9K 15.4K 153
Rate this:
Please Sign up or sign in to vote.
This article explains an easy implementation of an ASP.NET DateSelector User Control.

Sample screenshot

Fig 1: DateSelector User Control

Introduction

Date selection in a data entry interface or report selection interface is a repeating feature in any development environment. A common requirement might be providing a popup calendar using which the user can pick a date and load a date text box.

This article focuses on a simple solution for ASP.NET development environment which encapsulates a JavaScript popup calendar (ASP.NET Image control), an ASP.NET TextBox control and an ASP.NET Label controlling the form of a user control.

Requirements

If we think about a date selection control, it mainly consists of three components:

  1. An image or button when clicked displays a calendar to select a date.
  2. A text box that holds the selected date value.
  3. A label that describes the date ("Date" or "Start Date" or "End Date" etc..)

Following diagram shows the same structure in live environment. We will see the implementation of this structure as a user control very soon. I deliberately chose not to use the ASP.NET Calendar control because of postbacks.

Sample screenshot

Fig 2 : Structure of DateSelector

Design & Source code Structure

The source code consists of a DateSelector.ascxfile which contains the structure shown in Fig2 and a UseDateSelector.aspx file which gives an idea on how to use the control.

Using the code

Download the source code by clicking here. Install the MSI package provided. If you have not provided any Virtual Directory name during the installation, all the sources will be installed under the path C:\inetpub\wwwroot\DateSelectorControl. There are only two source code files, one is DataSelector.ascx and the other is UseDateSelector.aspx which is a sample file on how to use this DateSelector user control. All the images and JavaScript functions for pop up calendar are under a folder called cal.

DateSelector.ascx file contains two properties called text and calendarDate. User can use these properties either at design time or in the code behind.

The critical code components of both these files are listed below:

DateSelector.ascx

' Get the id of the control rendered on client side
' Very essential for Javascript Calendar scripts to recognize the textbox
Public Function getClientID() As String
    Return txt_Date.ClientID()
End Function
' This property sets/gets the calendar date
Public Property CalendarDate() As String
    Get
        Return txt_Date.Text
    End Get
    Set(ByVal Value As String)
        txt_Date.Text = Value
    End Set
End Property
' This Property sets or gets the the label for 
' Dateselector user control
Public Property Text() As String
    Get
        Return lbl_Date.Text
    End Get
    Set(ByVal Value As String)
        lbl_Date.Text = Value
    End Set
End Property

To use the user control in any WebForm, register the control and then assign default properties as shown below.

UseDateSelector.aspx

Register the control and start using it within the <form> tag as shown below:

1.    <%@Register TagPrefix="SControls" TagName="DateSelector"
       src="DateSelector.ascx" %>

2.    <SCONTROLS:DateSelector id="useDateCal" runat="server"
       Text="Start Date:"></SCONTROLS:DateSelector>

      <SCONTROLS:DateSelector id="dtCal" runat="server"
      Text="End Date:"></SCONTROLS:DateSelector>

How it Works?

The imgCalendar (or the calendar image you are seeing in Fig.2) is an ASP.NET Image control and is attached with a JavaScript "onclick" event as shown below. Whenever this image is clicked, the JavaScript takes care of loading the text box with the selected date. This user control also works in DataGrid columns. Observe the way I am passing the clientID of the control using the getClientID() instead of the normal ID of the TextBox control.

Dim scriptStr As String
  = _ "javascript:return popUpCalendar(this," & getClientID() &;
      ", 'mm/dd/yyyy', '__doPostBack(\'" & getClientID() & "\')')"
       imgCalendar.Attributes.Add("onclick", scriptStr)

How to Use ?

The attached ZIP (Demo) file contains an MSI package. Install the package and modify the source code to suit your needs. If you let the installation package to install at default locations then you can access the output at:

http://localhost/DateSelectorControl/DateSelector/UseDateSelector.aspx

If you specify the "Virtual Directory" as "DateSelectorDemo" (or any other name), then you can check the output at this URL:

http://localhost/DateSelectorDemo/DateSelector/UseDateSelector.aspx

After the installation is complete, if you want to modify/view sources, open Visual Studio .NET and open the project at this location:

http://localhost/DateSelectorControl or http://localhost/YourVirDirName

There are also VB.NET and C# source files attached as separate links. Download them by clicking the links at the top of this article.

Acknowledgements

Special Thanks to fuushikaden for his wonderful JavaScript calendar.

Future Improvements

  1. Make the Label position dynamic. Right now the Label is placed to the left of the TextBox. We may want to place the Label either at the top or bottom of the TextBox because of space constraints or for aesthetic reasons.
  2. Modify the JavaScript so that the user cannot select future dates.

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

Share

About the Author

venkriss
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionwhite empty space in front of textbox Pin
Member 105541541-Apr-14 2:00
memberMember 105541541-Apr-14 2:00 
QuestionLicense Information Pin
Member 1029127122-Sep-13 22:21
memberMember 1029127122-Sep-13 22:21 
QuestionThe iserted date will be reset???? Pin
nayomiranamuka17-Sep-13 20:28
membernayomiranamuka17-Sep-13 20:28 
NewsThis control is outdated, bute there is a new version available! (see link inside) Pin
Simon B.2-Dec-11 23:37
memberSimon B.2-Dec-11 23:37 
GeneralMy vote of 5 Pin
Member 831779228-Oct-11 2:01
memberMember 831779228-Oct-11 2:01 
QuestioniLike Pin
Member 831779228-Oct-11 2:01
memberMember 831779228-Oct-11 2:01 
GeneralMy vote of 4 Pin
mohammed nazer23-Oct-11 21:43
membermohammed nazer23-Oct-11 21:43 
GeneralThanks a lot Venkriss Pin
parth_sha16-Aug-10 4:05
memberparth_sha16-Aug-10 4:05 
Questionlicense Pin
Charlie Price10-Feb-10 2:30
memberCharlie Price10-Feb-10 2:30 
GeneralI have More easy way to use Calendar Pin
Himanshu Verma8-Jan-09 0:58
memberHimanshu Verma8-Jan-09 0:58 
GeneralGetting the control working on Firefox Pin
Frosty3323-Dec-08 9:09
memberFrosty3323-Dec-08 9:09 
GeneralRe: Getting the control working on Firefox Pin
Frosty335-Jan-09 3:38
memberFrosty335-Jan-09 3:38 
GeneralFantastic job Pin
PaulDBarker20-Dec-08 4:58
memberPaulDBarker20-Dec-08 4:58 
GeneralHelp Out to run this Code... Pin
rmsohaila5-Aug-08 1:06
memberrmsohaila5-Aug-08 1:06 
GeneralRe: Help Out to run this Code... Pin
binoy rajan8-Aug-08 13:35
memberbinoy rajan8-Aug-08 13:35 
GeneralRe: Help Out to run this Code... Pin
mabpak15-Aug-08 2:33
membermabpak15-Aug-08 2:33 
Generaltxt_date loses value on Postback Pin
Anie Varghese28-Jul-08 23:55
memberAnie Varghese28-Jul-08 23:55 
QuestionThis is not working in firefox. Pin
srinivasaganesan9-Jul-08 15:47
membersrinivasaganesan9-Jul-08 15:47 
AnswerRe: This is not working in firefox. Pin
moongarden21-Dec-08 3:06
membermoongarden21-Dec-08 3:06 
GeneralRe: This is not working in firefox. Pin
Frosty3323-Dec-08 9:17
memberFrosty3323-Dec-08 9:17 
GeneralRe: This is not working in firefox. Pin
moongarden23-Dec-08 11:47
membermoongarden23-Dec-08 11:47 
GeneralRe: This is not working in firefox. Pin
yummyinmytummy2-Apr-09 13:04
memberyummyinmytummy2-Apr-09 13:04 
QuestionHow to load the calendar when the page is first loaded Pin
samiji29-Dec-07 8:53
membersamiji29-Dec-07 8:53 
GeneralUp-to-date version for VS2005 supporting latest browsers available here Pin
Member 20931168-Dec-07 3:27
memberMember 20931168-Dec-07 3:27 
Questionstartdate enddate Pin
Fekri Abdullah30-Oct-07 11:41
memberFekri Abdullah30-Oct-07 11:41 

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 | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 10 Mar 2005
Article Copyright 2005 by venkriss
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid