Asp.net2.0里Calendar控件的使用

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值