Friday 23 October 2015

Highlight Saturday,Sunday or weekend dates in calendar control in asp.net



Description:-



In this example we explain that how to Highlight Saturday,Sunday weekend dates in calendar control in asp.net C#, VB. Or Disable weekend dates or saturaday Sunday in calendar in asp.net. this is very useful example when you want to user enter only working hours date not holiday dates. So you can highlight or disable weekend dates or saturaday and Sunday in calendar as per your requirement. So there are multiple ways to highlight or disable or checked weekend days in calendar are as follows.


Calender.aspx:-



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="calender.aspx.cs" Inherits="kiritblog.calender" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Hide Weekend days like sunday in calender in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
     <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>

    </form>
</body>
</html>


Calender.aspx.cs:-

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kiritblog
{
    public partial class calender : System.Web.UI.Page
    {
        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;
            }
        }
    }
}

 


 

0 comments:

Post a Comment