Highlight date range in Calendar control based on database date using C# and VB.Net in ASP.Net

Last Reply 11 days ago By dharmendr

Posted 11 days ago

How do I alter this code to be able to highlight the days between the FromDate and ToDate as it currently only highlights the FromDate  and ToDate.

    Protected Sub Calendar1_DayRender1(sender As Object, e As DayRenderEventArgs)
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("##").ConnectionString)
                conn.Open()
            Dim cmd As New SqlCommand("SELECT FromDate, ToDate FROM tblLeaveRequest WHERE Department = @Department", conn)
            cmd.Parameters.AddWithValue("@Department", txtDepartment.Text)
            Dim adapter As New SqlDataAdapter(cmd)
                Dim tbl As New DataTable()
            adapter.Fill(tbl)
            If tbl.Rows.Count > 0 Then 
                Dim FromDate = tbl.Rows(0)(0)
                Dim ToDate = tbl.Rows(0)(1)
                For Each row As Data.DataRow In tbl.Rows 
                    If Convert.ToDateTime(e.Day.Date) = Convert.ToDateTime(row("FromDate")) Or Convert.ToDateTime(e.Day.Date) = Convert.ToDateTime(row("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
            conn.Close() 
            End Using 
        End Sub

 

 

Posted 11 days ago

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