Calendar.OnDayRender(TableCell, CalendarDay) Método

Definición

Provoca el evento DayRender del control Calendar y permite proporcionar un controlador personalizado para el evento DayRender.

protected:
 virtual void OnDayRender(System::Web::UI::WebControls::TableCell ^ cell, System::Web::UI::WebControls::CalendarDay ^ day);
protected virtual void OnDayRender (System.Web.UI.WebControls.TableCell cell, System.Web.UI.WebControls.CalendarDay day);
abstract member OnDayRender : System.Web.UI.WebControls.TableCell * System.Web.UI.WebControls.CalendarDay -> unit
override this.OnDayRender : System.Web.UI.WebControls.TableCell * System.Web.UI.WebControls.CalendarDay -> unit
Protected Overridable Sub OnDayRender (cell As TableCell, day As CalendarDay)

Parámetros

cell
TableCell

Objeto TableCell que contiene información sobre la celda que se va a representar.

day
CalendarDay

Objeto CalendarDay que contiene información sobre el día que se va a representar.

Ejemplos

En el ejemplo de código siguiente se muestra cómo especificar y codificar un controlador para el DayRender evento para que el color de fondo sea amarillo para los días del mes mostrado. También muestra cómo personalizar el contenido de una celda agregando un System.Web.UI.LiteralControl control a la celda.

<%@ 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="VB" 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="VB" runat="server">
   
        Sub DayRender(source As Object, e As DayRenderEventArgs)
            
            ' Change the background color of the days in the month
            ' to yellow.
            If Not e.Day.IsOtherMonth And Not e.Day.IsWeekend Then
                e.Cell.BackColor = System.Drawing.Color.Yellow
            End If 
            ' Add custom text to cell in the Calendar control.
            If e.Day.Date.Day = 18 Then
                e.Cell.Controls.Add(New LiteralControl(ChrW(60) & "br" & ChrW(62) & "Holiday"))
            End If 
        End Sub 'DayRender 

   </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>
<%@ Page Language="VB" 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">
   
      Sub DayRender(sender as Object, e As DayRenderEventArgs) 

         ' Change the background color of the days in the month
         ' to yellow.
         If (Not e.Day.IsOtherMonth) And (Not e.Day.IsWeekend) Then
        
            e.Cell.BackColor=System.Drawing.Color.Yellow
         
         End If

         ' Add custom text to cell in the Calendar control.
         If e.Day.Date.Day = 18 Then
         
            e.Cell.Controls.Add(New LiteralControl("<br />Holiday"))
         
         End If

      End Sub

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Manually register the event-handling method for the DayRender  
         ' event of the Calendar control.
         AddHandler Calendar1.DayRender, AddressOf DayRender

      End Sub

   </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>

Comentarios

Aunque no se admite el enlace de datos para el Calendar control, es posible modificar el contenido y el formato de las celdas de fecha individuales. Antes de que el Calendar control se muestre en la página Web, crea y ensambla los componentes que componen el control. El DayRender evento se genera cuando se crea cada celda de fecha del Calendar control. Puede controlar el contenido y el formato de una celda de fecha cuando se crea proporcionando código en el controlador de eventos para el DayRender evento.

El controlador de eventos recibe un DayRenderEventArgs objeto que contiene datos de eventos. El DayRenderEventArgs objeto contiene dos propiedades que se pueden usar para controlar mediante programación el formato de la celda de fecha. La Cell propiedad representa la celda que se representa, mientras que la Day propiedad representa la fecha que se va a representar en la celda.

También puede personalizar el contenido de una celda agregando controles dinámicamente a la Control.Controls colección de la Cell propiedad .

Nota

Dado que el DayRender evento se genera mientras se representa el Calendar control, no se puede agregar un control que también pueda generar un evento, como LinkButton. Solo puede agregar controles estáticos, como System.Web.UI.LiteralControl, Label, Imagey HyperLink.

Cuando se genera un evento, se invoca el controlador de eventos a través de un delegado. Para obtener más información, consulte controlar y provocar eventos.

El método OnDayRender también permite que las clases derivadas controlen el evento sin adjuntar ningún delegado. Ésta es la técnica preferida para controlar el evento en una clase derivada.

Notas a los desarrolladores de herederos

Al reemplazar OnDayRender(TableCell, CalendarDay) en una clase derivada, asegúrese de llamar al método OnDayRender(TableCell, CalendarDay) de la clase base para que los delegados registrados reciban el evento.

Se aplica a

Consulte también