Calendar Web 服务器控件声明性语法

更新:2007 年 11 月

显示一个月历,该月历允许用户选择日期和移动到下一个月或上一个月。

<asp:Calendar
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    Caption="string"
    CaptionAlign="NotSet|Top|Bottom|Left|Right"
    CellPadding="integer"
    CellSpacing="integer"
    CssClass="string"
    DayNameFormat="Full|Short|FirstLetter|FirstTwoLetters|Shortest"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    FirstDayOfWeek="Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|
        Saturday|Default"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
                Large|X-Large|XX-Large"
        Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    NextMonthText="string"
    NextPrevFormat="CustomText|ShortMonth|FullMonth"
    OnDataBinding="DataBinding event handler"
    OnDayRender="DayRender event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelectionChanged="SelectionChanged event handler"
    OnUnload="Unload event handler"
    OnVisibleMonthChanged="VisibleMonthChanged event handler"
    PrevMonthText="string"
    runat="server"
    SelectedDate="string"
    SelectionMode="None|Day|DayWeek|DayWeekMonth"
    SelectMonthText="string"
    SelectWeekText="string"
    ShowDayHeader="True|False"
    ShowGridLines="True|False"
    ShowNextPrevMonth="True|False"
    ShowTitle="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    TitleFormat="Month|MonthYear"
    ToolTip="string"
    UseAccessibleHeader="True|False"
    Visible="True|False"
    VisibleDate="string"
    Width="size"
>
        <DayHeaderStyle/>
        <DayStyle/>
        <NextPrevStyle/>
        <OtherMonthDayStyle/>
        <SelectedDayStyle/>
        <SelectorStyle/>
        <TitleStyle/>
        <TodayDayStyle/>
        <WeekendDayStyle/>
</asp:Calendar>

备注

Calendar 控件用于显示月历,该月历允许用户选择日期和移动到下一个月和上一个月。

通过设置 SelectionMode 属性,可以指定用户是否可以选择一天、一周或一个月,还可以完全禁用日期选择。

Calendar 控件的不同部分设置样式属性可自定义该控件的外观。下表列出 Calendar 控件的不同样式属性。

样式对象

说明

Style 类

DayHeaderStyle

月历中显示一周中每一天的名称的部分的样式。

TableItemStyle

DayStyle

所显示的月份中各天的样式。

说明:

通过分别设置 WeekendDayStyleTodayDayStyleSelectedDayStyle 属性,可以使周末、当前日期和选定日具有不同的样式。

TableItemStyle

NextPrevStyle

标题栏左右两端的月导航 LinkButton 控件所在部分的样式。

TableItemStyle

OtherMonthDayStyle

显示在当前月视图中的上一个月和下一个月的日期样式。

TableItemStyle

SelectedDayStyle

选定日期的样式。

说明:

如果未设置此属性,则使用 DayStyle 属性指定的样式显示选定的日期。

TableItemStyle

SelectorStyle

位于 Calendar 控件左侧、包含用于选择一周或整个月的链接的列的样式。

TableItemStyle

TitleStyle

位于月历顶部、包含月份名称和月导航链接的标题栏的样式。

说明:

如果设置了 NextPrevStyle,则它将重写位于标题栏两端的下一个月和上一个月导航控件的样式。

TableItemStyle

TodayDayStyle

当前日期的样式。

说明:

如果未设置此属性,则使用 DayStyle 属性指定的样式显示当前日期。

TableItemStyle

WeekendDayStyle

周末日期的样式。

说明:

如果未设置此属性,则使用 DayStyle 属性指定的样式显示周末日期。

TableItemStyle

还可通过显示或隐藏 Calendar 控件的不同部分来控制该控件的外观。下表列出 Calendar 控件中可显示或隐藏的各个部分。

属性

说明

ShowDayHeader

显示或隐藏显示一周中的每一天的部分。

ShowGridLines

显示或隐藏一个月中的每一天之间的网格线。

ShowNextPrevMonth

显示或隐藏到下一个月或上一个月的导航控件。

ShowTitle

显示或隐藏标题部分。

尽管 Calendar 控件不支持绑定到数据源,但仍然可以修改各个日期单元格的内容和格式设置。在网页上显示 Calendar 控件之前,它将创建并装配构成该控件的组件。在创建 Calendar 控件中的每个日期单元格时,均会引发 DayRender 事件。通过在 DayRender 事件的事件处理程序中提供代码,可以在创建日期单元格时控制其内容和格式设置。

注意   Calendar 控件在客户端浏览器上呈现 JavaScript。客户端浏览器必须启用了 JavaScript,此控件才能正常运行。有关客户端脚本的更多信息,请参见 ASP.NET 网页中的客户端脚本

有关每个样式类支持的属性的信息,请参见样式对象属性

有关 Calendar 控件的属性和事件的详细信息,请参见 Calendar 类的文档。

示例

下面的示例演示在 .aspx 文件中声明 Calendar 控件的示例。该声明包含许多样式对象属性,并建立 Date_Selected 方法作为 SelectionChanged 事件的处理程序。

<asp:Calendar id="Calendar2"
     OnSelectionChanged="Date_Selected"
     SelectionMode="DayWeekMonth"
     Font-Name="Verdana" 
     Font-Size="12px"
     NextPrevFormat="ShortMonth"
     SelectWeekText="week"
     SelectMonthText="month"
     runat="server">

   <TodayDayStyle Font-Bold="True"/>
   <DayHeaderStyle Font-Bold="True"/>
   <OtherMonthDayStyle ForeColor="gray"/>
   <TitleStyle BackColor="#3366ff"
               ForeColor="white"
               Font-Bold="True"/>

   <SelectedDayStyle BackColor="#ffcc66"
                     Font-Bold="True"/>
   <NextPrevStyle ForeColor="white"
                  Font-Size="10px"/>
   <SelectorStyle BackColor="#99ccff" 
                  ForeColor="navy"
                  Font-Size="9px"/>
</asp:Calendar>

下面的示例演示 Calendar 控件的 SelectionChanged 事件的事件处理方法。通过查询 Calendar 控件的 SelectedDates 参数,可以确定选定的天数,进而确定用户选择的是一天、一周还是一个月。有关该选择的信息显示在 Label Web 服务器控件中。

Sub Date_Selected(sender as Object sender, e As EventArgs)
   Select (Calendar1.SelectedDates.Count)
      Case 0:    'None
         Label1.Text = "No dates are currently selected"
      Case 1:    'Day
         Label1.Text = "The selected date is " & _ 
                       Calendar1.SelectedDate.ToShortDateString
      Case 7:    'Week
         Label1.Text = "The selection is a week beginning " & _
                       Calendar1.SelectedDate.ToShortDateString
      Case Else: 'Month
         Label1.Text = "The selection is a month beginning " & _
                       Calendar1.SelectedDate.ToShortDateString
   End Select
End Sub
protected void Date_Selected(object sender, EventArgs e)
{
   switch (Calendar1.SelectedDates.Count)
   {
      case (0):   //None
      {
         Label1.Text = "No dates are currently selected";
         break;
      }
      case (1):   //Day
      {
         Label1.Text = "The selected date is " + 
                       Calendar1.SelectedDate.ToShortDateString();
         break;
      }
      case (7):   //Week
      {
         Label1.Text = "The selection is a week beginning " + 
                       Calendar1.SelectedDate.ToShortDateString();
         break;
      }
      default:    //Month
         Label1.Text = "The selection is a month beginning " + 
                       Calendar1.SelectedDate.ToShortDateString();
         break;
   }
}

请参见

概念

Calendar Web 服务器控件概述

ASP.NET 服务器控件

参考

Calendar

其他资源

Web 服务器控件语法