VS.NET(C#)-4.7_Calendar控件

本文详细介绍了ASP.NET中Calendar控件的使用方法,包括如何响应SelectionChanged等关键事件、如何利用DayRender自定义日期显示及如何通过VisibleMonthChanged监测月份变化等。此外还提供了示例代码帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Calendar控件

主要事件

l  SelectionChanged

l  DayRender

l  VisibleMonthChanged

protected void Calendar1_SelectionChanged(object sender, EventArgse)

    {

        lblTodaysDate.Text = "Today's Date is :"+ Calendar1.TodaysDate.ToShortDateString();

        if(Calendar1.SelectedDate != DateTime.MinValue)

        {

            lblSelected.Text = "选择日期: " +Calendar1.SelectedDate.ToShortDateString();

        }

        lblCount.Text = "已选天数:"+Calendar1.SelectedDates.Count.ToString();

        lbl01.Text= Calendar1.SelectedDate.DayOfYear.ToString();

   }

Calendar1.TodaysDate属性、SelectedDate属性均会返回System.DateTime类型的值

DateTime类具有多个方法将DateTime对象转换为其他格式

ToFileTime

ToLongDateString

ToLongTimeString

ToShortTimeString

ToString

 

DateTime的只读属性

名称

类型

说明

Date

DateTime

返回月份中的日期

Day

Integer

 

DayOfWeek

DayOfWeek

 

DayOfYear

Integer

 

Hour

Integer

 

Minute

Integer

 

Month

Integer

 

Second

Integer

 

Ticks

Long

 

TimeOfDay

TimeSpan

 

Year

Integer

返回年部分

 

VisibleDat

        显示月份的日期

        protected void Page_Load(objectsender, EventArgs e)

    {

        if(!IsPostBack)

        {

            cld.VisibleDate = cld.TodaysDate;

            cld.SelectedDate = cld.TodaysDate;

            ddl.SelectedIndex =cld.VisibleDate.Month - 1;

        }

   }

       

        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)

    {

        cld.SelectedDates.Clear();

        //通过下拉列表构造需要显示的月份的日期

        cld.VisibleDate = new DateTime(cld.VisibleDate.Year,Int32.Parse(ddl.SelectedItem.Value),1);

        //cld.SelectedDate= cld.VisibleDate;

   }

 

DayOfWeek  显示一个中所有星期五

        protected void btnTGIF_Click(objectsender, EventArgs e)

    {

        intcurrentMonth = cld.VisibleDate.Month;

        intcurrentYear = cld.VisibleDate.Year;

        cld.SelectedDates.Clear();

        for(int i = 1; i <System.DateTime.DaysInMonth(currentYear,currentMonth);i++)

        {

            DateTimev_Date = new DateTime(currentYear,currentMonth,i);

            if(v_Date.DayOfWeek == DayOfWeek.Friday)

            {

                cld.SelectedDates.Add(v_Date);

            }

        }

   }

SelectedDates.SelectRange  选择范围

        protected void btnRange_Click(objectsender, EventArgs e)

    {

        intcurrentMonth = cld.VisibleDate.Month;

        intcurrentYear = cld.VisibleDate.Year;

        DateTimestartDate = new DateTime(currentYear,currentMonth,Int32.Parse(txtStart.Text));

        DateTimeendDate = new DateTime(currentYear,currentMonth, Int32.Parse(txtEnd.Text));

        cld.SelectedDates.Clear();

       cld.SelectedDates.SelectRange(startDate,endDate);

   }

 

DayRender事件

        在Calendar控件呈现到客户端浏览器之前,将创建该控件的所有组件,随着创建每个单元格,将引发DayRender事件

DayRender事件处理程序接收两个DayRenderEventArgs类型参数。该对象有两个属性

Ø  Cell  表示单元格对象

Ø  Day  表示呈现在单元格中日期的CalendarDay对象

protected void cld_DayRender(objectsender, DayRenderEventArgs e)

    {

        //在单元格中显示Happy birthday

        if(e.Day.Date.Month==8 && e.Day.Date.Day == 9)

        {

            e.Cell.Controls.Add(new LiteralControl("<br/> Happy Birthday!"));

        }

        //显示春节快乐!

        if(e.Day.Date.Month == 2 && e.Day.Date.Day == 15)

        {

            e.Cell.Controls.Add(new LiteralControl("<br/> 春节快乐!"));

        }

    }

VisibleMonthChanged事件

        确定用户是否更改了月份。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值