namespace CommonFunction
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		DataSel 的摘要说明。
	/// </summary>
	public abstract class DataSel : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox txtYear;
		protected System.Web.UI.WebControls.Panel panlCalendar;
		protected System.Web.UI.WebControls.LinkButton btnYear;
		protected System.Web.UI.WebControls.Calendar dataCal;
		private void Page_Load(object sender, System.EventArgs e)
		{
			//移除当前Calendar控件的选定日期
			dataCal.SelectedDates.Clear();
			if(!IsPostBack)
				hideCalendar();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		///		设计器支持所需的方法 - 不要使用
		///		代码编辑器修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnYear.Click += new System.EventHandler(this.btnYear_Click);
			this.dataCal.SelectionChanged += new System.EventHandler(this.dataCal_SelectionChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnYear_Click(object sender, System.EventArgs e)
		{
			int year,month,day;
			year = Convert.ToInt32(txtYear.Text.Trim());
			month = DateTime.Now.Month;
			day = DateTime.Now.Day;
			DateTime querydate = new DateTime(year,month,day);
			dataCal.VisibleDate = querydate;
		}

		public void displayCalendar( string displayTextBox, DateTime dSelectedDate, string sDateFieldName , string iTop , string iLeft )
		{
			if (panlCalendar.Visible == false)
			{
				panlCalendar.Style["top"] = iTop;
				panlCalendar.Style["left"] = iLeft;
				dataCal.SelectedDate = dSelectedDate;
				dataCal.VisibleDate = dSelectedDate;
				dataCal.ToolTip = displayTextBox;
				dataCal.Attributes["SelectedField"] = sDateFieldName;
				panlCalendar.Visible = true;
			}
			else
				hideCalendar();
		}

		public void hideCalendar()
		{
			panlCalendar.Visible = false;
			
		}


		private void dataCal_SelectionChanged(object sender, System.EventArgs e)
		{
			TextBox txtDate; 
			txtDate = (TextBox)Page.FindControl(dataCal.Attributes["SelectedField"]);
			txtDate.Text = dataCal.SelectedDate.ToShortDateString();
			hideCalendar();
		}
	}
}
