欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-01 20:46:40
1、介绍 (1、在calender中,所有可选择的符号会显示下划线,这是因为它们在浏览器都会呈现为链接。      如...

1、介绍

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

     如果让用户可以选择某天、月、周,必须设置selectionmode属性(day、 dayweek、dayweekmonth)

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

 

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

     通过  calendar1.selecteddate.toshortdatestring();来获取所选择的时间点
     通过  calendar1.selecteddate.count.tostring();来获取所选择的天数

2、实例

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

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

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

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

 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" %>

 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
     <title>calender  control</title>
 </head>
 <body>
     <form id="form1" runat="server">
     <div>
         <h1>calender  control</h1>
         <h2></h2>
         <asp:calendar id="my_calendar" runat="server"
                     onselectionchanged="calendar_select">
         </asp:calendar>
         <br  />
         <asp:label id="lblcount" runat="server" text="label"></asp:label>
         <br />
         <asp:label id="lbltodaysdate" runat="server" text="label"></asp:label>
         <br />
         <asp:label id="lblselcteddate" runat="server" text="label"></asp:label>

         <table>
            <tr>
                <td>select a month</td>
                <td>
                    <asp:dropdownlist id="month_list" runat="server"
                        autopostback="true"
                        onselectedindexchanged="month_selectedchange">
                        <asp:listitem text="january" value="1" />
                        <asp:listitem text="february" value="2" />
                        <asp:listitem text="march" value="3" />
                        <asp:listitem text="april" value="4" />
                        <asp:listitem text="may" value="5" />
                        <asp:listitem text="june" value="6" />
                        <asp:listitem text="july" value="7" />
                        <asp:listitem text="augut" value="8" />
                        <asp:listitem text="september" value="9" />
                        <asp:listitem text="october" value="10" />
                        <asp:listitem text="november" value="11" />
                        <asp:listitem text="december" value="12" />
                    </asp:dropdownlist>
                </td>
                <td>
                    <asp:button id="btntgif" runat="server"
                        text="tgif"
                        onclick="btntgif_click"/>
                </td>
            </tr>
            <tr>
                <td  colspan="2"> </td>
            </tr>
            <tr>
                <td  colspan="2"><b>day range</b></td>
            </tr>

            <tr>
                <td >starting  day</td>
                <td >ending  day</td>
            </tr>

            <tr>
                <td >
                    <asp:textbox id="txtstart" runat="server"
                        width="25" maxlength="2"></asp:textbox>
                </td>
                <td >
                    <asp:textbox id="txtend" runat="server"
                        width="25" maxlength="2"></asp:textbox>
                </td>
                <td >
                    <asp:button id="btnrange" runat="server" text="apply"
                      onclick="btnrange_click" style="height: 21px"/>
                </td>
            </tr>
         </table>
     </div>
     </form>
 </body>
 </html>

总结: 

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

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

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

             e.day.isothermonth    e.day.isweekend  等