Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Adding Calendar Popup to Text Controls

0.00/5 (No votes)
19 Sep 2005 1  
Adding Calendar popup on doubleclick

Sample Image - DateSelector.jpg

Introduction

Well I'm back with a new small usercontrol. a few days ago one of my customers asked me if it was possible to give a user the possibility of either writing a date in a textfield or just doubleclick it to get a Datepicker, so i made this control. This control adds the Date pick to Labels , TextBox, HTML inputfields and Divs. What it does is when bound to the mentioned controls is adding the ability of double clik a control and the popup a DatePicker. you can have as many datepickers on a page as you like and it easy to change the look of the control by using VS Calandar properties. To use the control just add the control to your project by using the add existing in solution explorer. select the files "Remember the close.jpg". Then drag the control to the page where you want to use it, and set the properties.

You must set the following 3 properties on the DateSelector control.

DateFormat="dd-MM-yyyy". This property set the dateformat to your liking, defaults to localdatetime if not set.

ParentControlId="TextBox1". The id of the control you which to bind to.

PrevDate="true". Is it allowed to select a date previous to today's date. When this is set to false the control sets the date to todays date if date selected is older than todays date.

The javascript is tested to run in Opera 8.0, Firefox 1.06 and IE 6+

The Demo project provided show the use of the control.

NEW:

I have updated the control since it used absolute positioning on parent controls

Now it will open the calendar according to mouse positioning and you can place the close.jpg anywhere in your project

// Programmer Jan Nielsen

// Date: 02/08/2005

namespace LivingTest

{

using System;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.IO;

/// <summary>

/// Summary description for DateSelector.

/// </summary>

public class DateSelector : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.Calendar Calendar1;

protected DirectoryInfo info;

private bool monthChanged = false;

private string cName;

private string cNameClientId;

private string controlId ;

private string dateFormat = DateTime.Now.ToString();

protected System.Web.UI.HtmlControls.HtmlImage IMG1;

private string imgUrl;

private bool prevDate = false;

private string Date;

private string top;

private string left;

private string imgPath = "";

private void Page_Load(object sender, System.EventArgs e)

{

controlId=this.ID+cName;

Calendar1.VisibleDate = DateTime.Today;

setImg();

}

// finds and set the closebutton image on popup

private void setImg()

{

DirectoryInfo i = new DirectoryInfo(Request.PhysicalApplicationPath);

getDir(i);

imgPath = imgPath.Replace(i.Root.ToString(),"");

imgPath = imgPath.Replace(@"\","/");

imgPath = imgPath.Remove(0,imgPath.IndexOf('/'));

imgUrl = imgPath + "/close.jpg";

}

// Dont know were you place your usercontrol so i look in all dirs on the website to find it

// returns the path of the close button

private void getDir(DirectoryInfo inf)

{

foreach(DirectoryInfo i in inf.GetDirectories())

{

if(i.GetFiles("DateSelector.ascx").Length > 0)

imgPath = i.FullName;

if(i.GetDirectories().Length > -1)

getDir(i);

}

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.Calendar1.VisibleMonthChanged += new System.Web.UI.WebControls.MonthChangedEventHandler(this.Calendar1_VisibleMonthChanged);

this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

/// <summary>

/// Set Date format defaults to Local DateTimeFormat if not set

/// </summary>

public string DateFormat

{

set{dateFormat = value;}

get{return dateFormat;}

}

/// <summary>

/// navnet p� den control hvor dato skal vises

/// </summary>

public string ParentControlId

{

set{cName = value;}

}

/// <summary>

/// returns the controlId to javascript a composite id so each datetime control gets a unik Id

/// set in pageload

/// </summary>

public string ControlId

{

get{return controlId;}

}

/// <summary>

/// Set the value for the close image

/// </summary>

public string ImgUrl

{

set{imgUrl = value;}

get{ return imgUrl;}

}

/// <summary>

/// allows or disallows to chose a date older than todays date

/// </summary>

public bool PrevDate

{

set{prevDate = value;}

}

/// <summary>

/// ensure that if we change month the control is still visible

/// </summary>

public void showDate()

{

if(monthChanged)

Response.Write("<script language="'javascript'">document.getElementById('"+controlId+"').style.visibility = 'visible';</script>");

}

/// <summary>

/// left position of the control

/// </summary>

public string posLeft

{

set{left = value;}

get{ return left;}

}

/// <summary>

/// right position of the control

/// </summary>

public string posTop

{

set{top = value;}

get{ return top;}

}

/// <summary>

/// get the TextControl used by javascript

/// </summary>

public string ControlClientId

{

get{

Object s = new Object();

s = (Object)this.Parent.FindControl(cName);

if(s.GetType() == typeof(Label))

{

cNameClientId = ((Label)s).ClientID;

}

if(s.GetType() == typeof(TextBox))

{

cNameClientId = ((TextBox)s).ClientID;

}

if(s.GetType() == typeof(HtmlInputText))

{

cNameClientId = ((HtmlInputText)s).ClientID;

}

if(s.GetType() == typeof(HtmlGenericControl))

{

cNameClientId = ((HtmlGenericControl)s).ClientID;

}

return cNameClientId;

}

}

/// <summary>

/// writes the selected date back to the ParentControl

/// </summary>

private string setDate

{

set{

Object s = new Object();

s = (Object)this.Parent.FindControl(cName);

if(s.GetType() == typeof(Label))

{

((Label)s).Text = value;

cNameClientId = ((Label)s).ClientID;

}

if(s.GetType() == typeof(TextBox))

{

((TextBox)s).Text = value;

cNameClientId = ((TextBox)s).ClientID;

}

if(s.GetType() == typeof(HtmlInputText))

{

((HtmlInputText)s).Value = value;

cNameClientId = ((HtmlInputText)s).ClientID;

}

if(s.GetType() == typeof(HtmlGenericControl))

{

((HtmlGenericControl)s).InnerHtml = value;

cNameClientId = ((HtmlGenericControl)s).ClientID;

}

Date = value;

}

}

/// <summary>

/// just calling setDate with the selected value

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Calendar1_SelectionChanged(object sender, System.EventArgs e)

{

monthChanged = false;

if(prevDate)

{

setDate = ((Calendar)sender).SelectedDate.ToString(dateFormat);

return;

}

if(((Calendar)sender).SelectedDate >= DateTime.Now )

{

setDate = ((Calendar)sender).SelectedDate.ToString(dateFormat);

}

else

{

setDate = DateTime.Now.ToString(dateFormat);

}

}

/// <summary>

/// set the value to monthChanged if changing months

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void Calendar1_VisibleMonthChanged(object sender, System.Web.UI.WebControls.MonthChangedEventArgs e)

{

monthChanged = true;

}

}

}

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