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;
}
}
}