Controls You Can Use on Web Forms ASP.NET Standard Controls Calendar Control
By default, days in the Calendar 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:
- Programmatically highlight certain days, for example, showing holidays in a different color.
- Programmatically specify whether an individual day can be selected.
- Add information into the day display, such as appointment or event information.
When the Calendar control is creating the output to send to the browser, it raises an DayRender event you can handle. The control calls your method 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 DayRender event method 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:
- Cell, which you can use to set the appearance of an individual day.
- Day, which you can use to query information about the day being rendered, control whether the day can be selected, and add content to a day. The Day object supports various properties you can use to learn about the day ( for example, IsSelected, IsToday, and so on ) . It also supports a Controls collection that you can manipulate to add content to the day.
- Create a method for the Calendar control’s DayRender event. The event should have the following signature:
Public Sub Calendar1_DayRender ( ByValControls As Object, _
ByVal e As DayRenderEvent )
- In the method, set properties of the Cell object available via the DayRenderEvent argument, as in the following example:
Set e.Cell.BackColor = Colors.Red
The following example shows a simple but complete method that illustrates how to change the appearance of individual days. The method causes every other day in the calender to be rendered in green; alternating days are rendered in the default day color.
public Sub Calendar1_DayRender ( ByVal sender as Object, _
ByVal 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.Parse ( "Green" )
EndIf
End With
End Sub
- In a method for the Calendar control’s DayRender event, determine what day is being rendered by getting information from the Day object in the DayRenderEvent argument. Specifically, query the Date property of the Day object. Then set that day’s IsSelectable property to true. In the following example, the date July 4, 2000 is made selectable.
public Sub Calendar1_DayRender ( ByVal sender as Object, _
ByVal e as DayRenderEventArgs )
myHoliday = new DateTime ( 2000, 7, 4 )
If e.Day.Date = myHoliday Then
e.Day.IsSelectable = True
EndIf
End Sub
- In a method for the Calendar control’s DayRender event, add any valid Web Forms page element ( including HTML ) to the Controls collection of the Day object from the DayRenderEvent argument.
- The following example displays holidays. The holidays are created as a two-dimensional array during page load; holiday descriptions are loaded into the elements corresponding to their date. In the DayRender event method, each day is compared against the holiday array. If the corresponding element of the holiday array contains something, a label control is created with the holiday text and added into the Controls collection for that day.
Dim Holidays ( 1 to 12, 1 to 31 ) as String
' Load holidays array during page load
Public Sub Page1_Load ( ByVal 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_DayRender ( ByVal sender as Object, _
ByVal 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