Calendar.OnDayRender メソッド
アセンブリ: System.Web (system.web.dll 内)
構文
Protected Overridable Sub OnDayRender ( _ cell As TableCell, _ day As CalendarDay _ )
データ連結は 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>
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- Calendar.OnDayRender メソッドのページへのリンク