Calendar.OnDayRender メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Calendar.OnDayRender メソッドの意味・解説 

Calendar.OnDayRender メソッド

Calendar コントロールの DayRender イベントを発生させて DayRender イベントのカスタム ハンドラを指定できるようにします。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

解説解説

データ連結は Calendar コントロールではサポートされていませんが、各日付セルの内容と書式は変更できます。Calendar コントロールは、Web ページに表示される前に、構成要素であるコンポーネントを作成してアセンブルします。DayRender イベントは、Calendar コントロールの各日付セルが作成されるときに発生します。DayRender イベントのイベント ハンドラにコードを記述することで、日付セルの作成時にその内容と書式を制御できます。

イベント ハンドラは、イベント データが格納されている DayRenderEventArgs オブジェクトを受け取ります。DayRenderEventArgs オブジェクトには、日付セルの書式をプログラムにより制御するためにユーザーが使用できる 2 つのプロパティが格納されています。Cell プロパティはセルが表示されることを表しますが、Day プロパティはセルに日付が表示されることを表します。

Cell プロパティの Control.Controls コレクションにコントロールを動的に追加することにより、セルの内容をカスタマイズすることもできます。

メモメモ

DayRender イベントは Calendar コントロールの表示中に発生するため、ユーザーはイベントを発生させる可能性がある LinkButton などのコントロールを追加できません。追加できるのは System.Web.UI.LiteralControl、Label、Image、HyperLink などのスタティック コントロールだけです。

イベントが発生すると、デリゲートを使用してイベント ハンドラが呼び出されます。詳細については、「イベントとデリゲート」を参照してください。

OnDayRender メソッドを使用すると、デリゲートを結び付けずに、派生クラスでイベントを処理することもできます。派生クラスでイベントを処理する場合は、この手法をお勧めします。

継承時の注意 派生クラスで OnDayRender をオーバーライドする場合は、登録されているデリゲートがイベントを受け取ることができるように、基本クラスの OnDayRender メソッドを呼び出してください。

使用例使用例

DayRender イベントのハンドラを指定およびコード化して、表示された月の日付の背景色を黄色にする方法を次のコード例に示します。このコード例では、セルに System.Web.UI.LiteralControl コントロールを追加して、セルの内容をカスタマイズする方法も示します。

<%@ Page Language="VB" AutoEventWireup="True"
 %>
<html>
<head>

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

   <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 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="JScript" AutoEventWireup="True" %>
<html>
<head>

   <script language="JScript" runat="server">
   
      function DayRender(source : Object, e : DayRenderEventArgs)
 
      {

         // 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 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"
 %>
<html>
<head>

   <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 runat="server">

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

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

      </asp:Calendar>
                   
   </form>
          
</body>
</html>
   
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>

   <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 runat="server">

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

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

      </asp:Calendar>
                   
   </form>
          
</body>
</html>
   
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

Calendar.OnDayRender メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Calendar.OnDayRender メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS