|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Fig 1: DateSelector User Control IntroductionDate 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 RequirementsIf we think about a date selection control, it mainly consists of three components:
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 Fig 2 : Structure of DateSelector Design & Source code StructureThe 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 codeDownload 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.ascx file contains two properties called 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.aspxRegister the control and start using it within the 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 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. AcknowledgementsSpecial Thanks to fuushikaden for his wonderful JavaScript calendar. Future Improvements
|
||||||||||||||||||||||