Introduction

In this article, I will show you, how to add holiday details within control and apply some style for those dates. The purpose of this article is add India holiday details within the calendar control and apply some style to those dates.

Implementation

First design simple html page with calendar control as like following.

HTML Code

HTML / XML
<body>
 <form id="form1" runat="server">
 <div> <div> 
<p style="text-align: center"> <b>
 <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial Black" Font-Size="Medium"
 ForeColor="#0066FF">Indian List of Holidays 2011</asp:Label>
<br /></b> </p> 
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
 BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
 ForeColor="#663399" ShowGridLines="True" OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged"
 OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">
 <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
 <SelectorStyle BackColor="#FFCC66" />
 <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
 <OtherMonthDayStyle ForeColor="#CC9966" />
 <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
 <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
 <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
 </asp:Calendar> <br /> <b></b> 
<asp:Label ID="LabelAction" runat="server"></asp:Label>
<br /> </b> 
</div> 
</div>
 </form> 
</body>

Then add server side code to mark the holidays in calendar object.

C# Code

Code
using System;
Code
using System.Configuration;
Code
using System.Data;
Code
using System.Linq;
Code
using System.Web;
Code
using System.Web.Security;
Code
using System.Web.UI;
Code
using System.Web.UI.HtmlControls;
Code
using System.Web.UI.WebControls;
Code
using System.Web.UI.WebControls.WebParts;
Code
using System.Xml.Linq;
Code
using System.Collections;
Code
public partial class calender : System.Web.UI.Page
Code
{
Code
    Hashtable HolidayList;
Code
    protected void Page_Load(object sender, EventArgs e)
Code
    {
Code
        HolidayList = Getholiday();
Code
        Calendar1.Caption = "Calender - Author: Espranza";
Code
        Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
Code
        Calendar1.NextPrevFormat = NextPrevFormat.FullMonth;
Code
        Calendar1.TitleFormat = TitleFormat.MonthYear;
Code
        Calendar1.ShowGridLines = true;
Code
        Calendar1.DayStyle.Height = new Unit(50);
Code
        Calendar1.DayStyle.Width = new Unit(50);
Code
        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Center;
Code
        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Middle;
Code
        Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.AliceBlue;
Code
    }
Code
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
Code
    {
Code
        if (HolidayList[e.Day.Date.ToShortDateString()] != null)
Code
        {
Code
            Literal  literal1 = new Literal();
Code
            literal1.Text = "<br/>";
Code
            e.Cell.Controls.Add(literal1);
Code
            Label label1 = new Label();
Code
            label1.Text = (string)HolidayList[e.Day.Date.ToShortDateString()];
Code
            label1.Font.Size = new FontUnit(FontSize.Small);
Code
            label1.Font.Bold = true;
Code
            e.Cell.Controls.Add(label1);
Code
        }
Code
    }
Code
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
Code
    {
Code
        LabelAction.Text = "Date changed to :" + Calendar1.SelectedDate.ToShortDateString();
Code
    }
Code
    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
Code
    {
Code
        LabelAction.Text = "Month changed to :" + e.NewDate.ToShortDateString();
Code
    }
Code
    private Hashtable Getholiday()
Code
    {
Code
        Hashtable holiday = new Hashtable();
Code
        holiday["1/1/2011"] = "New Year";
Code
        holiday["1/5/2011"] = "Guru Govind Singh Jayanti";
Code
        holiday["1/8/2011"] = "Muharam (Al Hijra)";
Code
        holiday["1/14/2011"] = "Pongal";
Code
        holiday["1/26/2011"] = "Republic Day";
Code
        holiday["2/23/2011"] = "Maha Shivaratri";
Code
        holiday["3/10/2011"] = "Milad un Nabi (Birthday of the Prophet";
Code
        holiday["3/21/2011"] = "Holi";
Code
        holiday["3/21/2011"] = "Telugu New Year";
Code
        holiday["4/3/2011"] = "Ram Navmi";
Code
        holiday["4/7/2011"] = "Mahavir Jayanti";
Code
        holiday["4/10/2011"] = "Good Friday";
Code
        holiday["4/12/2011"] = "Easter";
Code
        holiday["4/14/2011"] = "Tamil New Year and Dr Ambedkar Birth Day";
Code
        holiday["5/1/2011,5/1/2011"] = "May Day";
Code
        holiday["5/9/2011"] = "Buddha Jayanti and Buddha Purnima";
Code
        holiday["6/24/2011"] = "Rath yatra";
Code
        holiday["8/13/2011"] = "Krishna Jayanthi";
Code
        holiday["8/14/2011"] = "Janmashtami";
Code
        holiday["8/15/2011"] = "Independence Day";
Code
        holiday["8/19/2011"] = "Parsi New Year";
Code
        holiday["8/23/2011"] = "Vinayaka Chaturthi";
Code
        holiday["9/2/2011"] = "Onam";
Code
        holiday["9/5/2011"] = "Teachers Day";
Code
        holiday["9/21/2011"] = "Ramzan";
Code
        holiday["9/27/2011"] = "Ayutha Pooja";
Code
        holiday["9/28/2011"] = "Vijaya Dasami (Dusherra)";
Code
        holiday["10/2/2011"] = "Gandhi Jayanti";
Code
        holiday["10/17/2011"] = "Diwali & Govardhan Puja";
Code
        holiday["10/19/2011"] = "Bhaidooj";
Code
        holiday["11/2/2011"] = "Guru Nanak Jayanti";
Code
        holiday["11/14/2011"] = "Children's Day";
Code
        holiday["11/28/2011"] = "Bakrid";
Code
        holiday["12/25/2011"] = "Christmas Day";
Code
        holiday["12/28/2011"] = "Muharram";
Code
        return holiday;
Code
    }
Code
}