栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

ASP.NET笔记之Calender的使用说明

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ASP.NET笔记之Calender的使用说明

1、介绍

(1、在Calender中,所有可选择的符号会显示下划线,这是因为它们在浏览器都会呈现为链接。

     如果让用户可以选择某天、月、周,必须设置SelectionMode属性(Day、 DayWeek、DayWeekMonth)

                            

 

(2   控件事件  当用户选择了某一天或者月,可以用OnSelectionChanged来触发

     通过  Calendar1.SelectedDate.ToShortDateString();来获取所选择的时间点
     通过  Calendar1.SelectedDate.Count.ToString();来获取所选择的天数

2、实例

现在通过一个实例来加深对日历控件的理解:

当点击TGIF时,会在日历上显示所选月份的所有星期五

当点击Apply时,会在日历上显示开始到结束的日期

                    

 Calender.aspx.cs
复制代码 代码如下:
using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;

 public partial class myTest_Calender : System.Web.UI.Page
 {
     protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack) {
             my_Calendar.VisibleDate = my_Calendar.TodaysDate;
             //将选择的日期中的月份映射到下拉框中
             Month_List.SelectedIndex = my_Calendar.VisibleDate.Month - 1;
         }
         //显示到标签中
         lblTodaysDate.Text = "Today is  :" + my_Calendar.TodaysDate.ToShortDateString();
     }
     //选择一个日期的时候触发函数
     protected void Calendar_Select(object sender, EventArgs e)
     {
         lblCountUpdate();
         lblSelectedUpdate();
         txtClear();
     }
     //下拉框触发函数
     protected void Month_SelectedChange(object sender, EventArgs e)
     {
         my_Calendar.SelectedDates.Clear();
         lblSelectedUpdate();
         lblCountUpdate();
         //重新设置时间
         my_Calendar.VisibleDate = new DateTime(my_Calendar.VisibleDate.Year,
             Int32.Parse(Month_List.SelectedItem.Value), 1);
         txtClear();
     }

     //重构函数01-修改日期的次数
     private void lblCountUpdate() {
         lblCount.Text = "the Count of Selected:" + my_Calendar.SelectedDates.Count.ToString();      
     }
     //重构函数02-清楚数据
     private void txtClear() {
         txtEnd.Text = "";
         txtStart.Text = "";
     }
     //重构函数03-修改日期
     private void lblSelectedUpdate() {
         if (my_Calendar.SelectedDate != DateTime.MinValue)
             lblSelctedDate.Text = "the  selected day is :" +
                 my_Calendar.SelectedDate.ToShortDateString();
     }
     //按钮1:显示所选月份的所有星期五
     protected void btnTgif_Click(object sender, EventArgs e)
     {
         int currnetMonth = my_Calendar.VisibleDate.Month;
         int curretnYear = my_Calendar.VisibleDate.Year;
         //先清除原先日历位置
         my_Calendar.SelectedDates.Clear();
         //如果日期是星期五则将其添加到日历中
         for (int i = 1; i <= System.DateTime.DaysInMonth(
             curretnYear, currnetMonth); i++) {

                 DateTime datetime = new DateTime(curretnYear, currnetMonth, i);
                 if (datetime.DayOfWeek == DayOfWeek.Friday)
                     my_Calendar.SelectedDates.Add(datetime);
             }
         lblSelectedUpdate();
         lblCountUpdate();
     }
     //按钮2:
     protected void btnRange_Click(object sender, EventArgs e)
     {
         int currnetMonth = my_Calendar.VisibleDate.Month;
         int curretnYear = my_Calendar.VisibleDate.Year;

         DateTime StartDate = new DateTime(curretnYear, currnetMonth,Int32.Parse(txtStart.Text));
         DateTime EndtartDate = new DateTime(curretnYear, currnetMonth, Int32.Parse(txtEnd.Text));
         my_Calendar.SelectedDates.Clear();
         my_Calendar.SelectedDates.SelectRange(StartDate,EndtartDate);

         lblCountUpdate();
         lblSelectedUpdate();
     }
 }

Calender.aspx
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calender.aspx.cs" Inherits="myTest_Calender" %>

 

 
 
     Calender  Control
 
 
    
 
 

总结: 

(1  采用一些重构,将一些函数方法分离出去,这一块有一些还没分离完全

(2  日期控件还有VisiblMonthChanged事件来处理日历是否被用户更改了月份,   e.NewDate.Year 和e.PreviousDate.Year诸如此类的比较

(3 DayRender事件,可以通过cell和Day来呈现日期的特殊性,例如周末和节假日的颜色,  

             e.Day.IsOtherMOnth    e.Day.IsWeekend  等

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/57772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号