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事件
确定用户是否更改了月份。