使用英语阅读

通过


Calendar.OnDayRender(TableCell, CalendarDay) 方法

定义

引发 DayRender 控件的 Calendar 事件并使您可以为 DayRender 事件提供自定义处理程序。

protected virtual void OnDayRender(System.Web.UI.WebControls.TableCell cell, System.Web.UI.WebControls.CalendarDay day);

参数

cell
TableCell

一个 TableCell,包含有关要呈现的单元格的信息。

day
CalendarDay

一个 CalendarDay,包含有关要呈现的日的信息。

示例

下面的代码示例演示如何为 事件指定和编码处理程序, DayRender 使所显示月份中的日期的背景色为黄色。 它还演示了如何通过向单元格添加 System.Web.UI.LiteralControl 控件来自定义单元格的内容。

<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
    <title>DayRender Event Example</title>
<script language="C#" runat="server">
   
      void DayRender(Object source, DayRenderEventArgs e) 
      {

         // Change the background color of the days in the month
         // to yellow.
         if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
            e.Cell.BackColor=System.Drawing.Color.Yellow;

         // Add custom text to cell in the Calendar control.
         if (e.Day.Date.Day == 18)
            e.Cell.Controls.Add(new LiteralControl("<br />Holiday"));

      }

   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">

      <h3>DayRender Event Example</h3>
 
      <asp:Calendar id="calendar1" 
                    OnDayRender="DayRender"
                    runat="server">

         <WeekendDayStyle BackColor="gray">
         </WeekendDayStyle>

      </asp:Calendar>
                   
   </form>
          
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>Calendar DayRender Example</title>
<script runat="server">
   
      void DayRender(Object sender, DayRenderEventArgs e) 
      {

         // Change the background color of the days in the month
         // to yellow.
         if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
         {
            e.Cell.BackColor=System.Drawing.Color.Yellow;
         }

         // Add custom text to cell in the Calendar control.
         if (e.Day.Date.Day == 18)
         {
            e.Cell.Controls.Add(new LiteralControl("<br />Holiday"));
         }

      }

      void Page_Load(Object sender, EventArgs e)
      {

         // Manually register the event-handling method for the DayRender  
         // event of the Calendar control.
         Calendar1.DayRender += new DayRenderEventHandler(this.DayRender);

      }

   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">

      <h3>Calendar DayRender Example</h3>
 
      <asp:Calendar id="Calendar1" 
                    runat="server">

         <WeekendDayStyle BackColor="gray">
         </WeekendDayStyle>

      </asp:Calendar>
                   
   </form>
          
</body>
</html>

注解

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

事件处理程序接收 DayRenderEventArgs 包含事件数据的 对象。 对象 DayRenderEventArgs 包含两个可用于以编程方式控制日期单元格格式的属性。 属性 Cell 表示要呈现的单元格,而 Day 属性表示在单元格中呈现的日期。

还可以通过向 属性的集合Cell动态添加控件来Control.Controls自定义单元格的内容。

备注

由于 在 DayRender 呈现控件时 Calendar 引发 事件,因此不能添加也可以引发事件的控件,例如 LinkButton。 只能添加静态控件,例如 System.Web.UI.LiteralControlLabelImageHyperLink

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnDayRender 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnDayRender(TableCell, CalendarDay) 时,一定要调用基类的 OnDayRender(TableCell, CalendarDay) 方法,以便已注册的委托对事件进行接收。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅