using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Calendar : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
//连接数据库,填充数据集DataTable
SqlConnection conn = new SqlConnection("server=192.168.0.17;uid=sa;pwd=sa;database=calendar");
conn.Open();
SqlDataAdapter dad = new SqlDataAdapter("select * from cal", conn);
dad.Fill(dt);
//获取请求页面的参数id
string strId = this.Request.QueryString["id"];
//首次加载时不需绑定GridView;注意,若是空值就不要ToString(),否则得到的是“ ”空字符串,会引发空指针异常
if (strId != null)
{
//把DataTable绑定到DataView里
DataView dv = new DataView(dt);
//根据calendarId筛选DataView里的值并绑定到GridView中
dv.RowFilter = "calendarId=" + strId;
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
}
//设置在Calendar控件上要显示的月份的日期,防止还原到当前的月份的日期
if (this.Session["NewDate"]!= null) {
this.Calendar1.VisibleDate=(DateTime)this.Session["NewDate"];
}
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
//根据数据集里的数据设置Calendar控件呈现的外观,
foreach(DataRow dr in dt.Rows){
//判断当前选中的日期是否在数据集,并各自添加一个hyperlink和一个listeralControl两个看控件
if (e.Day.Date.ToString() == dr["cal_time"].ToString()) {
HyperLink hl = new HyperLink();
hl.NavigateUrl = this.Request.CurrentExecutionFilePath +"?id=" + dr["calendarId"].ToString();
hl.Text = dr["cal_short"].ToString();
LiteralControl lc = new LiteralControl("<br/>");
e.Cell.Controls.Add(hl);
e.Cell.Controls.Add(lc);
}
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
//筛选当前选中的日期所得到的数据绑定到GridView里
DataView dv = new DataView(dt);
dv.RowFilter = "calendarId=" + this.Calendar1.SelectedDate.Date.ToString();
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
}
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{ //设置月份的值,保存到Session里;
this.Session["NewDate"] = this.Calendar1.VisibleDate;
}
}