By default, days in the Calendar Web control are simply displayed as numbers. (If day selection is enabled, the numbers appear as links. For details, see Controlling User Date Selection.) However, you can customize the appearance and content of individual days, which allows you to:
When the Calendar control is creating the output to send to the browser, it raises an OnDayRender event you can handle. The control calls your handler for each day as it is preparing the day for display, and you can then programmatically examine which date is being rendered and customize it appropriately.
The OnDayRender event handler takes two arguments, a reference to the control raising the event (the calendar control) and an object of type DayRenderEvent. The DayRenderEvent object provides access to two additional objects:
To customize the appearance of an individual day
[Visual Basic]
Public Sub Calendar1_DayRender(s As Object, _
e As DayRenderEvent)
[Visual Basic]
Set e.Cell.BackColor = Colors.Red
The following example shows a simple but complete handler that illustrates how to change the appearance of individual days. The handler causes every other day in the calendar to be rendered in green; alternating days are rendered in the default day color.
[Visual Basic]
public Sub Calendar1_OnDayRender (sender as Object, _
e as DayRenderEventArgs)
With e
If Not .Day.IsOtherMonth And .Day.DayNumberText Mod 2 = 0 Then
' For even-numbered days in the current month,
' set the background color of the cell to green.
.Cell.BackColor = System.Drawing.Color.FromString("Green")
EndIf
End With
End Sub
To specify that an individual day can be selected
[Visual Basic]
public Sub Calendar1_OnDayRender (sender as Object, _
e as DayRenderEventArgs)
myHoliday = new DateTime(2000, 7, 4)
If e.Day.Date = myHoliday Then
e.Day.IsSelectable = True
EndIf
End Sub
To add content to an individual day
[Visual Basic]
Dim Holidays(1 to 12, 1 to 31) as String
' Load holidays array during page load
Public Sub Page1_OnLoad(e as EventArgs)
Holidays(1, 1) = "New Year's Day"
Holidays(2, 14) = "Valentine's Day"
Holidays(12, 25) = "Christmas Day"
End Sub
Public Sub Calendar1_OnDayRender(sender as Object, _
e as DayRenderEventArgs)
If e.DayCell.IsOtherMonth Then
e.DayCell.Controls.Clear
Else
Dim Hol as String
dt = new System.DateTime(e.DayCell.Date)
Hol = Holidays(dt.Month, dt.Day)
if Len(Hol) > 0 then
Dim labHoliday as New Label
labHoliday.Text = "<br>" & Hol
e.DayCell.Controls.Add labHoliday
End If
End If
End Sub
See Also