HOW TO:自訂 Calendar Web 伺服器控制項中的個別日期
更新:2007 年 11 月
根據預設,Calendar 控制項中的日期是以數字顯示。如果啟用日期選取,數字會以連結顯示。如需詳細資訊,請參閱 HOW TO:控制 Calendar Web 伺服器控制項中的使用者日期選取。
您可以自訂個別日期的外觀和內容,如此可讓您:
利用程式反白顯示特定日期,例如使用不同色彩來顯示假日。
利用程式指定個別日期是否可選取。
將資訊加入日期顯示,例如約會或活動資訊。
自訂使用者按一下可以選取日期的連結文字。
當 Calendar 控制項建立傳送給瀏覽器的輸出時,會引發 DayRender 事件。在準備顯示日期時,控制項會為每個日期引發事件,接著您就可以用程式的方式檢查呈現的日期,並適當地進行自訂。
DayRender 事件方法會使用兩個引數:一是引發事件的控制項 (Calendar 控制項) 參考,另一則是 DayRenderEventArgs 型別的物件。DayRenderEventArgs 物件能夠存取其他兩個物件:
Day,您可以用來查詢呈現的日期資訊、控制是否可以選取日期、以及將內容加入至日期。Day 物件支援各種屬性,您可以用這些屬性取得日期的資訊 (例如,IsSelected、IsToday 等等)。它也支援 Controls 集合,您可以用來將內容加入至日期。
若要自訂個別日期的外觀
在方法中,設定能夠使用 DayRenderEventArgs 引數存取之 Cell 物件的屬性。
下列範例顯示如何變更個別日期的外觀。這個方法讓日曆中的假期以黃色呈現,而週末日期則是以綠色呈現。在範例中,假日是 2005 年 11 月 23 日到 11 月 30 日。
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 If End Sub 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); }
若要指定個別日期為可選取的
在 Calendar 控制項的 DayRender 事件方法中,從 Day 物件的 Date 屬性取得資訊,以判斷要呈現的日期。
將此日期的 IsSelectable 屬性設定為 true。
下列範例顯示,如何將 2005 年 10 月 1 日設定為可選取,其他所有日期都設定為不可選取。
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 If End Sub
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 事件處理常式中,從 DayRenderEventArgs 引數將任何 HTML 或 ASP.NET Web 控制項加入至 Day 物件的 Controls 集合中。
以下範例將顯示假日。假日清單在載入頁面時,會建立為二維陣列。假日描述會對應其日期,載入項目中。在 DayRender 事件方法中,每個日期都會與假日陣列比較。如果假日陣列的對應項目包含值,則會以假日文字建立 Label 控制項,並將其加入至該日期的 Controls 集合中。
Dim holidays(13, 32) As String Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load holidays(1, 1) = "Birthday" holidays(2, 14) = "Anniversary" End Sub Protected 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 If End Sub
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"; }
若要自訂個別日期的連結文字
在 Calendar 控制項的 DayRender 事件方法中,取得 DayRenderEventArgs 引數的 SelectUrl 屬性。SelectUrl 屬性會傳回該日期正常呈現的 JavaScript,以產生代表日期選取的回傳。
使用串連建立 HTML 超連結,以便使用 SelectUrl 屬性 (Property) 的值當做 href 屬性 (Attribute)。
-
以下範例將顯示假日。假日清單在載入頁面時,會建立為二維陣列。假日描述會對應其日期,載入項目中。在 DayRender 事件方法中,每個日期都會與假日陣列比較。如果假日陣列的對應項目包含值,程式碼會建立顯示假日名稱的連結文字取代日期編號。
Dim holidays(13, 32) As String Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load holidays(1, 1) = "Birthday" holidays(2, 14) = "Anniversary" End Sub Protected 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 If End Sub
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"; }
請參閱
工作
HOW TO:在 Calendar 控制項中顯示來自資料庫的選取日期