日期選擇控件

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

 /// <summary>
 ///  DateBox 的摘要說明。
 /// </summary>
 public class  DateBox : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.TextBox Textbox2;
  protected System.Web.UI.WebControls.TextBox Textbox3;
  protected System.Web.UI.WebControls.LinkButton LinkButton1;
  protected System.Web.UI.WebControls.Calendar Calendar1;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.TextBox Tb_year;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  private static DateTime date;
  string[,] SpecialDays=new string[13,32];
  public  static DateTime Date
  {
   get
   {
    return date;
   }
  }
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置用戶代碼以初始化頁面
   if(!this.IsPostBack)
   {
   date=System.DateTime.Today;
   Tb_year.Text=System.DateTime.Today.Year.ToString();
   DropDownList1.SelectedValue=System.DateTime.Today.Month.ToString();
   }
   int Current_month=DateTime.Today.Month;
   int Current_day=DateTime.Today.Day;
   SpecialDays[Current_month,Current_day]="今天你有好運!";
   SpecialDays[1,1]="新年快樂";
   SpecialDays[2,14]="情人節";
   SpecialDays[4,1]="愚人節";
   SpecialDays[5,1]="國際勞動節";
   SpecialDays[5,4]="青年節";
   SpecialDays[6,1]="兒童節";
   SpecialDays[7,1]="建黨節";
   SpecialDays[8,1]="建軍節";
   SpecialDays[9,10]="教師節";
   SpecialDays[10,1]="國慶節";
   SpecialDays[12,25]="聖誕節";

  }

  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  ///  設計器支持所需的方法 - 不要使用代碼編輯器
  ///  修改此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
   this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
   this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged);
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void LinkButton1_Click(object sender, System.EventArgs e)
  {
     LinkButton1.Visible=false;
     Calendar1.Visible=true;
   Tb_year.Visible=true;
   DropDownList1.Visible=true;
   Label1.Visible=true;
   Label2.Visible=true;
   Button1.Visible=true;
  }

  private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
  {
   TextBox1.Text= Calendar1.SelectedDate.Year.ToString();
   Textbox2.Text= Calendar1.SelectedDate.Month.ToString();
   Textbox3.Text= Calendar1.SelectedDate.Day.ToString();
         date=Calendar1.SelectedDate;
   LinkButton1.Visible=true;
   Calendar1.Visible=false;
   Tb_year.Visible=false;
   DropDownList1.Visible=false;
   Label1.Visible=false;
   Label2.Visible=false;
   Button1.Visible=false;
  }

  
       
  private bool IsNumber(string s)
  {
   
   for(int i=0;i<s.Length;i++)
   {
    if(!Char.IsNumber(s[i]))
    return false;
   }
   return true;
  }

  private void Button1_Click(object sender, System.EventArgs e)
  {
   
   if(Tb_year.Text.Trim()!="" && IsNumber(Tb_year.Text.Trim()))
   {
    if(Tb_year.Text.Length==2)
    {
     Tb_year.Text="19"+Tb_year.Text;
    }
       Calendar1.SelectedDates.Clear();
    Calendar1.VisibleDate=new DateTime(int.Parse(Tb_year.Text.Trim())==0?1:int.Parse(Tb_year.Text.Trim()),int.Parse(DropDownList1.SelectedValue),1);
   
   }
  
  }

  private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
  {
    if(e.Day.IsOtherMonth)
     e.Cell.Controls.Clear();
    else
    {
     Style SpecialDayStyle =new Style();
            SpecialDayStyle.BackColor=System.Drawing.Color.DarkOrange;
   SpecialDayStyle.ForeColor=System.Drawing.Color.DarkBlue;
            SpecialDayStyle.BorderColor=System.Drawing.Color.Cyan;
   SpecialDayStyle.BorderWidth=new Unit(5);
      SpecialDayStyle.BorderStyle=BorderStyle.Ridge;
   SpecialDayStyle.Font.Size=FontUnit.Parse("8pt");

     try
     {
      string MyDay=SpecialDays[e.Day.Date.Month,e.Day.Date.Day];
      if(MyDay!=null)
      {
     if(MyDay.IndexOf("今天")>=0 && e.Day.Date.Year!=DateTime.Today.Year)
                     return;
       e.Cell.Controls.Add(new System.Web.UI.LiteralControl("<br>"+MyDay));
     e.Cell.ApplyStyle(SpecialDayStyle);
      }
     }
     catch
     {
      
     }
    }
  }
   

  
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章