ASP.NET 自定义Calendar Web 服务器控件

来源:互联网 发布:火车票什么软件最好 编辑:程序博客网 时间:2024/05/22 17:08
ASP.NET 自定义Calendar Web 服务器控件
2008-10-30 20:43

 

默认情况下,Calendar 控件中的日显示为数字。如果启用日选择,则数字将显示为链接。有关详细信息,请参见如何:控制 Calendar Web 服务器控件中的用户日期选定

但是,您可以自定义个别日的外观和内容,这包括执行下面的操作:

  • 以编程方式突出显示某些日。例如,以不同的颜色显示假日。

  • 以编程方式指定是否可以选定个别日。

  • 向日显示中添加信息,例如约会或事件信息。

  • 自定义用户可以单击以选择某日的链接文本。

Calendar 控件创建要发送到浏览器的输出时,它将引发 DayRender 事件。控件在准备要显示的日时将为每个日引发该事件,然后您可采用编程的方式检查正显示的是哪个日期,并对其进行适当的自定义。

DayRender 事件的方法带有两个参数,包括对引发事件的控件(Calendar 控件)的引用和一个 DayRenderEventArgs 类型的对象。DayRenderEventArgs 对象提供对另外两个对象的访问:

  • Cell,它是一个 TableCell 对象,可用于设置个别日的外观。

  • Day,可用于查询关于呈现日的信息,控制是否可选择该日,以及将内容添加到日中。Day 对象支持各种可用于了解有关日的信息的属性(例如,IsSelectedIsToday 等)。它还支持 Controls 集合,可操作该集合以将内容添加到日中。

自定义个别日的外观

  1. 创建用于处理 Calendar 控件的 DayRender 事件的方法。

  2. 在该方法中,设置 Cell 对象的属性,您可以使用 DayRenderEventArgs 参数访问此对象。

    下面的示例演示如何更改个别日的外观。该方法使日历中的节假日呈现为黄色,而周末呈现为绿色。在该示例中,节假日是 2005 年 11 月 23 日至 11 月 30 日。

    Visual Basic
    复制代码
    Protected Sub Calendar1_DayRender(ByVal sender As Object, _        ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender    ' Display vacation dates in yellow boxes with purple borders.    Dim vacationStyle As New Style()    With vacationStyle        .BackColor = System.Drawing.Color.Yellow        .BorderColor = System.Drawing.Color.Purple        .BorderWidth = New Unit(3)    End With    ' Display weekend dates in green boxes.    Dim weekendStyle As New Style()    weekendStyle.BackColor = System.Drawing.Color.Green    ' Vacation is from Nov 23, 2005 to Nov 30, 2005.    If ((e.Day.Date >= New Date(2005, 11, 23)) _            And (e.Day.Date <= New Date(2005, 11, 30))) Then        e.Cell.ApplyStyle(vacationStyle)    ElseIf (e.Day.IsWeekend) Then        e.Cell.ApplyStyle(weekendStyle)    End IfEnd Sub
    C#
    复制代码
    protected
    void Calendar1_DayRender(object sender, DayRenderEventArgs e){ // Display vacation dates in yellow boxes with purple borders. Style vacationStyle = new Style(); vacationStyle.BackColor = System.Drawing.Color.Yellow; vacationStyle.BorderColor = System.Drawing.Color.Purple; vacationStyle.BorderWidth = 3; // Display weekend dates in green boxes. Style weekendStyle = new Style(); weekendStyle.BackColor = System.Drawing.Color.Green; if ((e.Day.Date >= new DateTime(2000,11,23)) && (e.Day.Date <= new DateTime(2000,11,30))) { // Apply the vacation style to the vacation dates. e.Cell.ApplyStyle(vacationStyle); } else if (e.Day.IsWeekend) { // Apply the weekend style to the weekend dates. e.Cell.ApplyStyle(weekendStyle); }
    }

指定可选定个别日

  1. 在用于 Calendar 控件的 DayRender 事件的方法中,通过从 Day 对象的 Date 属性中获取信息来确定哪一日被呈现。

  2. 将该日的 IsSelectable 属性设置为 true。

    下面的示例演示如何将日期 2005 年 10 月 1 日设置为可选的,而所有其他日期都为不可选的。

    Visual Basic
    复制代码
    Protected Sub Calendar1_DayRender(ByVal sender As Object, _        ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender    Dim myAppointment As Date = New Date(2005, 10, 1)    If (e.Day.Date = myAppointment) Then        e.Day.IsSelectable = True    Else        e.Day.IsSelectable = False    End IfEnd Sub

     

    C#
    复制代码
    protected void Calendar1_DayRender(object sender,     DayRenderEventArgs e){    DateTime myAppointment = new DateTime(2005, 10, 1);    if (e.Day.Date == myAppointment)    {        e.Day.IsSelectable = true;    }    else    {        e.Day.IsSelectable = false;     }}

向个别日中添加内容

  • 在用于 Calendar 控件的 DayRender 事件的处理程序中,将任何 HTML 或 ASP.NET Web 控件添加到来自于 DayRenderEventArgs 参数的 Day 对象的 Controls 集合中。

    下面的示例显示假日。在页面加载过程中,将以二维数组创建假日列表。假日描述将被加载到与其日期对应的元素中。在 DayRender 事件的方法中,将每个日与假日数组进行比较。如果对应的假日数组元素中包含值,则使用假日文本创建一个 Label 控件,并将其添加到该日的 Controls 集合中。

    Visual Basic
    复制代码
    Dim holidays(13, 32) As StringProtected Sub Page_Load(ByVal sender As Object, _        ByVal e As System.EventArgs) Handles Me.Load    holidays(1, 1) = "Birthday"    holidays(2, 14) = "Anniversary"End SubProtected Sub Calendar1_DayRender(ByVal sender As Object, _        ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender    If e.Day.IsOtherMonth Then        e.Cell.Controls.Clear()    Else        Dim aDate As Date = e.Day.Date        Dim aHoliday As String = holidays(aDate.Month, aDate.Day)        If (Not aHoliday Is Nothing) Then            Dim aLabel As Label = New Label()            aLabel.Text = "<br>" & aHoliday            e.Cell.Controls.Add(aLabel)        End If    End IfEnd Sub

     

    C#
    复制代码
    string[,] holidays = new String[13, 32];protected void Calendar1_DayRender(object sender, DayRenderEventArgs e){    string aHoliday;    DateTime theDate = e.Day.Date;    aHoliday = holidays[theDate.Month, theDate.Day];    if (aHoliday != null)    {        Label aLabel = new Label();        aLabel.Text = " <br>" + aHoliday;        e.Cell.Controls.Add(aLabel);    }}protected void Page_Load(object sender, EventArgs e){    holidays[1, 1] = "Birthday";    holidays[2, 14] = "Anniversary";}

自定义个别日的链接文本

  1. 在用于 Calendar 控件的 DayRender 事件的方法中,获取 DayRenderEventArgs 参数的 SelectUrl 属性。SelectUrl 属性将返回 JavaScript,通常为该日呈现此 JavaScript 以引起指示日期选择的回发。

  2. 使用串联创建用 SelectUrl 属性的值作为 href 属性的 HTML 超链接。

  3. 将超链接添加为 Cell 对象的 Text 属性。

  4. 下面的示例显示假日。在页面加载过程中,将以二维数组创建假日列表。假日描述将被加载到与其日期对应的元素中。在 DayRender 事件的方法中,将每个日与假日数组进行比较。如果对应的假日数组元素包含值,则代码将创建显示假日名而非日编号的链接文本。

Visual Basic
复制代码
Dim holidays(13, 32) As StringProtected Sub Page_Load(ByVal sender As Object, _        ByVal e As System.EventArgs) Handles Me.Load    holidays(1, 1) = "Birthday"    holidays(2, 14) = "Anniversary"End SubProtected Sub Calendar1_DayRender(ByVal sender As Object, _        ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender    If e.Day.IsOtherMonth Then        e.Cell.Controls.Clear()    Else        Dim aDate As Date = e.Day.Date        Dim aHoliday As String = holidays(aDate.Month, aDate.Day)        If (Not aHoliday Is Nothing) Then            e.Cell.Text = _                "<a href=" & e.SelectUrl & ">" & aHoliday & "</a>"        End If    End IfEnd Sub

 

C#
复制代码
string[,] holidays = new String[13, 32];protected void Calendar1_DayRender(object sender, DayRenderEventArgs e){    string aHoliday;    DateTime theDate = e.Day.Date;    aHoliday = holidays[theDate.Month, theDate.Day];    if (aHoliday != null)    {        e.Cell.Text = "<a href=" + e.SelectUrl + ">" +            aHoliday + "</a>";    }}protected void Page_Load(object sender, EventArgs e){    holidays[1, 1] = "Birthday";    holidays[2, 14] = "Anniversary";}

 

原创粉丝点击