Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Friday 6 March 2015

While working with ASP.NET Calendar, it is required to highlight weekend dates (Saturday and Sunday). In this article i will explain How to mark/highlight weekend dates i.e. Saturday, Sunday or just Sunday on asp.net calendarcontrol in asp.net using both C# and VB languages.

Through this Article you can do the following tasks:
  1. Highlight Saturday, Sunday weekend dates in calendar.
  2. Highlight just Sunday dates in calendar i.e. marking holidays on calendar.
  3. Make calendar control look attractive by setting its attributes.

Source Code:
In the design page (default.aspx) place a calendar control and set its WeekendDayStyle attributes as highlighted below:

    <asp:Calendar ID="Calendar1"
         runat="server"
         Font-Names="verdana"          
         Font-Size="10pt"
         BorderColor="#CCCCCC"
         ShowGridLines="true"
         WeekendDayStyle-BackColor="#ffe900"
         WeekendDayStyle-ForeColor="#ff0000"
         NextPrevStyle-Font-Size="14pt"
         NextPrevStyle-ForeColor="#FFFFFF"
         DayHeaderStyle-BackColor="#E5E5E5"
         DayHeaderStyle-ForeColor="#000000"
         TitleStyle-Height="30px"        
         TitleStyle-BackColor="#525252"
         TitleStyle-Font-Size="18px"
         TitleStyle-ForeColor="#FFFFFF"
         ondayrender="Calendar1_DayRender">
     </asp:Calendar>

You can also highlight weekend dates through code as mentioned below. You can remove the WeekendDayStyle attributes from the calendar control because we are going to highlight weekend dates through code:


Asp.Net C# Code to highlight Saturday, Sunday on calendar control 
If you want to highlight weekend dates i.e. Saturday and Sunday on Calendar then In the code behind file (default.aspx.vb)  , write the code on DayRender event of calendar as:

 protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {       
        if (e.Day.IsWeekend)
        {
            e.Cell.BackColor = System.Drawing.Color.Yellow;
            e.Cell.ForeColor = System.Drawing.Color.Red;          
        }
    } 
Or you can also write the following code block. Output will be the same.

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {       
        if (e.Day.Date.DayOfWeek == DayOfWeek.Saturday || e.Day.Date.DayOfWeek == DayOfWeek.Sunday)
        {
            e.Cell.BackColor = System.Drawing.Color.Yellow;
            e.Cell.ForeColor = System.Drawing.Color.Red;
        }
    }

Now if  you want to highlight only Sunday dates on calendar then use this code :

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {       
        if (e.Day.Date.DayOfWeek == DayOfWeek.Sunday)
        {
            e.Cell.BackColor = System.Drawing.Color.Yellow;
            e.Cell.ForeColor = System.Drawing.Color.Red;
        }
    }

Asp.Net VB Code to highlight Saturday, Sunday on calendar control

<asp:Calendar ID="Calendar1"
         runat="server"
         Font-Names="verdana"           
         Font-Size="10pt"
         BorderColor="#CCCCCC"
         ShowGridLines="true"
        WeekendDayStyle-BackColor="#ffe900"
    WeekendDayStyle-ForeColor="#ff0000"
         NextPrevStyle-Font-Size="14pt"
         NextPrevStyle-ForeColor="#FFFFFF"
         DayHeaderStyle-BackColor="#E5E5E5"
         DayHeaderStyle-ForeColor="#000000"
         TitleStyle-Height="30px"         
         TitleStyle-BackColor="#0088BB"
         TitleStyle-Font-Size="18px"
         TitleStyle-ForeColor="#FFFFFF">
     </asp:Calendar>

 If you want to highlight weekend dates i.e. Saturday and Sunday on Calendar then In the code behind file (default.aspx.vb)  , write the code on DayRender event of calendar as:

 Protected Sub Calendar1_DayRender(sender As Object, e AsSystem.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If e.Day.IsWeekend Then
            e.Cell.BackColor = System.Drawing.Color.Yellow
            e.Cell.ForeColor = System.Drawing.Color.Red
        End If
    End Sub
  
Or you can also write the following code block. Output will be the same.

Protected Sub Calendar1_DayRender(sender As Object, e AsSystem.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If e.Day.Date.DayOfWeek = DayOfWeek.Saturday OrElse e.Day.Date.DayOfWeek = DayOfWeek.SundayThen
            e.Cell.BackColor = System.Drawing.Color.Yellow
            e.Cell.ForeColor = System.Drawing.Color.Red
        End If
    End Sub

Now suppose you want to highlight only Sunday dates on calendar then write the code as:

Protected Sub Calendar1_DayRender(sender As Object, e AsSystem.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If e.Day.Date.DayOfWeek = DayOfWeek.Sunday Then
            e.Cell.BackColor = System.Drawing.Color.Yellow
            e.Cell.ForeColor = System.Drawing.Color.Red
        End If

    End Sub

0 comments:

Post a Comment