Highlight Saturday,Sunday weekend dates in calendar control in asp.net C#, VB

highlight weekend dates on asp.net calendarIntroduction: In this article i am going to share How to mark/highlight weekend dates i.e. Saturday, Sunday or just Sunday on asp.net calendar control in asp.net using both C# and VB languages

Description: Basically in this article you will learn the following:
  • How to highlight Saturday, Sunday weekend dates in calendar.
  • How to highlight just Sunday dates in calendar i.e. marking holidays on calendar.
  • How to make calendar control look attractive by setting its attributes.


Implementation: Let's create a simple web page to demonstrate the concept.
  • 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="#0088BB"
         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 suppose you want to highlight only Sunday dates on calendar then write the code as:

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 As System.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 As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If e.Day.Date.DayOfWeek = DayOfWeek.Saturday OrElse 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

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

Protected Sub Calendar1_DayRender(sender As Object, e As System.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


 Now over to you:
" I hope you have got How to highlight weekend dates in Asp.Net Calendar control and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..