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

ASP.NET 4.0 TimePicker User Control

By , 25 Feb 2012
 

Download testTimePicker.zip - 1.23 MB

ScreenshotTimeEx1a.png

Introduction

This project is tested only on IE9, ASP.NET 4.0. The Time Picker User Control was developed exclusively for an Intranet site so cross-browser compatibility was not required. The User Control is written VB.NET.

This article shows how to create and add a custom Ajax Time Picker Extender User Control to an ASP.NET application. I didn't have the time to write a Ajax TimeExtender so I decided on using the Ajax Popup Extender and Datalist controls to quickly create a reusable Time Picker. I based the User Control style on the article by Farhan Ejaz that was incompatible with my ASP.NET 4.0 web application. I got the idea of building the Time Picker using an existing Ajax Control from the article by rperetz.

Background

I needed a quick Time Picker control as I began to upgrade existing projects to Dotnet 4.0 and finding my existing control was not compatible with Framework 4.0. I was surprised to find out that the existing Ajax Control Tool Kit did not include a Time Picker Control. Searching the internet didn't turn up any satisfactory Time Pickers so I decided to write a quick custom user control. Unfortunately, vb.net namespaces do not play nice with adding User Controls so an edit to the Inherits line has to made each time the control is added. There are some good articles that explain how to convert a User Control into a Server Control but I didn't have time to investigate this option.

Using the code

Download the project file from the link at the top of this article. Open the project only if you have already installed Visual Studio 2010 or above. The project is self contained with all the needed files and images to run the demo.

Remember to change the Inherits line in the ascx page to the root namespace of your project if adding just the User Control to an existing project.

Adding Time Picker Control to Parent Page

  1. Copy the User Control Directory to the Web Project.
  2. Edit the Inherits line in the aspx page of the User Control to point to your projects root namespace. This is usually the name of your VB.NET project.
  3. Compile the project.
  4. Add the code to register the User control on the parent page.
  5. Drag the Ajax Script Manager control and Update Panel onto the parent page.
  6. Add a Textbox to the parent page.
  7. Add the Time Picker Extended control using the tag prefix. Intellisense should show the control if it was added correctly to the project.
  8. Set the Time Picker's TargetControlID to the Textbox ID.
  9. Set the properties for the Time Picker control.
  10. Compile and run the project.
<%@ Register Src="CustomControls/TimeExtender.ascx" TagName="TimeExtender" TagPrefix="cc1" %> 

<asp:textbox runat="server" id="txtStartTime" />

<ccl:timeextender runat="server" targetcontrolid="txtStartTime" id="TimeExtender1">

Time Picker Control ASCX Source Code

The User Control main page was created by simply adding an Image Button, PopupControlExtender, and Datalist to the ascx page. The Datalist is populated by a dataview that contains the times. A MinuteInterval property allows the Time Picker to display different time intervals. The ShowAMandPM property allows the TimePicker to show only AM or PM or both. The current time is selected by clicking on the time in the heading. Note that there is no Ajax Script Manager or Update Panel on the ASCX page because the Parent page handles these functions.

<asp:ImageButton ID="ibtnShowPicker" 
    ImageUrl="btnTimeExtenderBlue.gif"
    OnClientClick="return false;"
    CssClass="btnTimeExtender_Style"
    runat="server" /> 

     <asp:DataList ID="datalistTimes"       
        RepeatDirection="Horizontal"
        RepeatColumns="4"
        OnItemDataBound="datalistTimes_OnItemDataBound"
        CssClass="Datalist_ControlStyle"
        HeaderStyle-CssClass="Datalist_HeaderStyle"
        ItemStyle-CssClass="Datalist_ItemStyle"
        runat="server" >

        <HeaderTemplate> 
            <div class="Datalist_HeaderLeft">      
            </div>
            <div class="Datalist_HeaderCenter">
            <asp:LinkButton ID="lbtnHeaderTime"
                OnCommand="lbtnHeaderTime_OnCommand"
                ToolTip="click to select current time"
                runat="server" />
            </div>
            <div class="Datalist_HeaderRight">
            <asp:LinkButton ID="lbtnHeaderAM" 
                Text="AM" 
                OnCommand="lbtnHeaderAM_OnCommand"  
                CommandArgument="am"
                runat="server" />
            <asp:LinkButton ID="lbtnHeaderPM" 
                Text="PM" 
                OnCommand="lbtnHeaderPM_OnCommand"
                CommandArgument="pm"
                runat="server" />
            </div>          
        </HeaderTemplate>
       
        <ItemTemplate>
        <asp:LinkButton ID="lbtnTime" 
            CommandName="second" 
            OnCommand="lbtnTime_Onclick" 
            CommandArgument='<%# Eval("Time") %>' 
            Text='<%# Eval("Time") %>'
            runat="server"/>         
        </ItemTemplate>
    </asp:DataList>  
   

Time Picker Control Code Behind

The code behind populates the Datalist control by adding times to a Dataset and using the Dataset as the Data Source. A Linkbutton control displays the times allowing the click events to be captured. Times are returned to the parent control using the Popup control's comment. A special routine (SetParentControlValue) had to be added to populate the parent control if a time button is displayed. Textboxes and labels are the only controls setup in the code. Additional controls must be added in the code behind in order for them to work with the Time Picker.

Conclusion

Additional Ajax controls can be created in the same way as the Time Picker control from existing Ajax controls. This technique speeds up development but causes extra work when adding custom controls to existing projects. All the files in the custom User Control must be copied to the project and the root namespace must be set to the projects namespace.

License

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

About the Author

BrianLaF
Web Developer
United States United States
Member
Started programming in Business Basic in the 1980's and acquired my AS in Computer Science at that time. Promoted to IS Manager after one year of programming (sink or swim).
Self taught in SMC Basic, Visual Basic, C Shell, perl, ASP, JavaScript, vb.net, asp.net.
Now working as a Network Administrator at a hospital in Northern California.
Fell into a webmaster role when implementing the company's intranet website to support documentation.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionShare C# code pleasememberChris Haarer4 Mar '13 - 7:02 
Please share the c# code with me please.... My email address is cj.haarer@gmail.com
 
Thank you kindly.
QuestionFrom where can I get newer Ajax Control Toolkit?memberHarshad.HBK6 Aug '12 - 22:55 
I am getting this error when I include Time Picker in my project.
 
Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
 
Please guide.
Thank You.
AnswerRe: From where can I get newer Ajax Control Toolkit?memberafzal.gujrat2 Jan '13 - 10:33 
try updating your Ajax tool kit assembly version it is using 4.1.50...
GeneralMy vote of 5memberPrasad_Kulkarni11 Jun '12 - 1:24 
Good work!
GeneralMy vote of 4member@k@ ?29 Feb '12 - 4:11 
Am able to drag drop the time into the other time and get some javascript:
 
javascript:__doPostBack('TimePicker1$datalistTimes$ctl10$lbtnTime','')
(both fields accept dragging the time unto each other)
 
Anyway, still great work!
QuestionNice JobmemberMember 287297823 Feb '12 - 11:37 
Thank you for the control. I wanted a time picker control other than a "NumericUpDown" type. This fits the bill excellently and performs as advertised. I am using it on a browser application that supports only IE. I translated it to C# VS2008 (.Net 3.5) for my purposes. I will share the C# source should anyone want it. I can report that the user control works fine on IE8 .net 3.5. It would probably work on any IE version >= 7. I did not try it on Firefox or Safari.
 
I have only one suggestion. On the "ibtnShowPicker" image button use "OnClientClick='return false;'" instead of "OnClientClick='this.disabled=true;'". This prevents an imagebutton postback without disabling the control and smoothes out the functionality of the image button. It also allows the user to toggle the time dropdown (popup) if desired.
AnswerRe: Nice JobmemberBrianLaF25 Feb '12 - 7:24 
I'm glad you found it useful. Thanks for the suggestion for the image button. I will test it and update the article.
brianl

AnswerRe: Nice Jobmemberjutiyi15 Aug '12 - 15:48 
Can share the c# code??thanks
GeneralRe: Nice JobmemberMember 287297815 Aug '12 - 16:01 
Hi. I would be happy to share the code. I am hoping that since I am "allowing private e-mail replies" to this that you will be able to send an e-mail address so that I can get you the .net 3.5 project. I don't think posting the code in text form in this message would be of much value and it doesn't appear that I can attach anything in this reply.
GeneralRe: Nice Jobmemberjutiyi15 Aug '12 - 21:14 
Ok..Thanks very much..my email is jutiyi@hotmail.com..

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 25 Feb 2012
Article Copyright 2012 by BrianLaF
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid