Adding preset Holidays to the displayed ASP.Net Calendar Control is really very simple. First, we need to creat a DayRender Event, which we call ‘Cal1_DayRender’, for this example. Then, we need to point to that DayRender Event, in the Control Tag itself (OnDayRender=”Cal1_DayRender”).
Then, we need to create an array (globally) with the max number of months and the max number of days possible:
Dim holidays(12, 31) As String
Then, inside the Page_Load Event, we just list the holidays we wish to have displayed automatically, using the month and the date. That’s all there is to it!
As an extra here, we can also see how to programmatically change the font and font size of a label, in the Cal1_DayRender Event.
<script language="VB" Runat="server">
Dim holidays(12, 31) As String
Sub Page_Load(Source as Object, E as EventArgs)
holidays(8, 5) = "Birthday"
holidays(8, 14) = "Anniversary"
holidays(1,1) = "New Year''s Day"
holidays(7,4)="The US Declares Independance"
holidays(12,25)="Christmas"
holidays(12,31)="New Year''s Eve"
End Sub
Sub Cal1_DayRender(sender As Object, _
e As DayRenderEventArgs)
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.Font.Name = "verdana"
aLabel.Font.Size = FontUnit.Point(10)
aLabel.Text = "<br>" & aHoliday
e.Cell.Controls.Add(aLabel)
End If
End If
End Sub
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 4.0">
<title>Holidays in the ASP.Net Calendar</title>
</head>
<body><asp:Label ID="PutIDNameHere" Runat="server" />
<form id="form1" Runat="server">
<asp:Calendar runat="server"
id="cal1"
Backcolor="White"
Forecolor="Black"
Borderwidth="2"
Width="75%"
ShowGridLines="true"
OnDayRender="Cal1_DayRender"
SelectedDayStyle-Backcolor="#DEEFFF"
SelectedDayStyle-forecolor="Black"
TitleStyle-BackColor="#DEEFFF"
TitleStyle-Font-Bold="True"
TitleStyle-Height="36px"
OtherMonthDayStyle-ForeColor="gray"
TodayDayStyle-Font-Bold="True"
TodayDayStyle-Font-Italic="True"
TodayDayStyle-Font-Size="12pt"
DayHeaderStyle-Font-Bold="True"
DayHeaderStyle-BackColor="LightGray" />
</form>
</body>
</html>