using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page 
{
    private System.Data.DataTable dt = null;

    public bool ValidateDate1(string input)
    {
        return Regex.IsMatch(input, "\\b(?<year>\\d{2,4})/(?<month>\\d{1,2})/(?<day>\\d{1,2})\\b");
    }
    public bool ValidateDate2(string input)
    {
        return Regex.IsMatch(input, "\\b(?<year>\\d{2,4})-(?<month>\\d{1,2})-(?<day>\\d{1,2})\\b");
    }
    public bool ValidateDate3(string input)
    {
        return Regex.IsMatch(input, "\\b(?<year>\\d{2,4})年(?<month>\\d{1,2})月(?<day>\\d{1,2})日\\b");
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        dt = new System.Data.DataTable();//构造一个表，保存日程信息
        dt.Columns.Add("date", typeof(DateTime));
        dt.Columns.Add("content");
        System.Data.DataRow dr = dt.NewRow();
        dr["date"] = DateTime.Now;
        dr["content"] = "8:30小会议室开会！";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["date"] = DateTime.Now.AddDays(3);
        dr["content"] = "15:30登录网站主页！";
        dt.Rows.Add(dr);
        if (!IsPostBack)
        {
            this.Calendar1.VisibleDate = DateTime.Now;//设置显示的日期
            this.Calendar1.DayNameFormat = DayNameFormat.Full;//设置周的星期几样式
            this.Calendar1.ShowGridLines = true;//显示网格线
            this.Calendar1.WeekendDayStyle.BackColor = System.Drawing.Color.GhostWhite;//设置周未日期的背景色
        }
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsOtherMonth)
        {
            e.Cell.Text = ""; //非本月单元格的样式
            return;
        }
        foreach (System.Data.DataRow dr in dt.Rows)
        {
            DateTime time = DateTime.Parse(dr["date"].ToString());
            if (time.Month != this.Calendar1.VisibleDate.Month)
            {
                continue;//如果不是本月的日就不作处理
            }
            if (time.Day.ToString() == e.Day.DayNumberText)//判断日期相同就显示日程信息
            {
                e.Cell.BackColor = System.Drawing.Color.LightBlue;
                e.Cell.Text = dr["content"].ToString();
                return;
            }
        }
        e.Cell.Text = e.Day.DayNumberText;//没有日程信息就把当天的日期赋值

    }
    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
    {
        this.TextBox1.Text = e.NewDate.ToShortDateString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (!ValidateDate1(TextBox1.Text) && !ValidateDate2(TextBox1.Text) && !ValidateDate3(TextBox1.Text))
            {
                Response.Write("<script>alert('输入的日期格式有误')</script>");
            }
            else
            {
                DateTime time = DateTime.Parse(this.TextBox1.Text);
                this.Calendar1.VisibleDate = time;
            }
        }
        catch
        {
            this.Calendar1.VisibleDate = DateTime.Now;
        }
    }
}
