Hi Tevin,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Calendar ID="Calendar1" OnDayRender="CalendarDayRender" runat="server"></asp:Calendar>
Code
C#
protected void CalendarDayRender(object sender, DayRenderEventArgs e)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] { new
System.Data.DataColumn("FromDate", typeof(DateTime)), new System.Data.DataColumn("ToDate", typeof(DateTime)) });
dt.Rows.Add(DateTime.Now.AddDays(-3), DateTime.Now);
dt.Rows.Add(DateTime.Now.AddDays(9), DateTime.Now.AddDays(15));
dt.Rows.Add(DateTime.Now.AddMonths(1).AddDays(1), DateTime.Now.AddMonths(1).AddDays(5));
if (dt.Rows.Count > 0)
{
foreach (System.Data.DataRow dr in dt.Rows)
{
DateTime fromDate = (DateTime)dr["FromDate"];
DateTime toDate = (DateTime)dr["ToDate"];
if (e.Day.Date >= fromDate && e.Day.Date <= toDate)
{
e.Cell.BackColor = System.Drawing.Color.Red;
e.Cell.ForeColor = System.Drawing.Color.White;
e.Cell.Font.Bold = true;
}
}
}
}
VB.Net
Protected Sub CalendarDayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
Dim dt As Data.DataTable = New Data.DataTable()
dt.Columns.AddRange(New Data.DataColumn() {
New Data.DataColumn("FromDate", GetType(DateTime)), New Data.DataColumn("ToDate", GetType(DateTime))})
dt.Rows.Add(DateTime.Now.AddDays(-3), DateTime.Now)
dt.Rows.Add(DateTime.Now.AddDays(9), DateTime.Now.AddDays(15))
dt.Rows.Add(DateTime.Now.AddMonths(1).AddDays(1), DateTime.Now.AddMonths(1).AddDays(5))
If dt.Rows.Count > 0 Then
For Each dr As Data.DataRow In dt.Rows
Dim fromDate As DateTime = CType(dr("FromDate"), DateTime)
Dim toDate As DateTime = CType(dr("ToDate"), DateTime)
If e.Day.Date >= fromDate AndAlso e.Day.Date <= toDate Then
e.Cell.BackColor = System.Drawing.Color.Red
e.Cell.ForeColor = System.Drawing.Color.White
e.Cell.Font.Bold = True
End If
Next
End If
End Sub
Screenshot