Calender with Holiday List in ASP.Net

Step 1: Add a calender control on webpage.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!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 id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Calendar ID="Calendar1" runat="server" Height="444px" Width="100%" OnDayRender="Calendar1_DayRender"

            OnSelectionChanged="Calendar1_SelectionChanged" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged"

            DayNameFormat="Full" ForeColor="#000099" NextMonthText="">

            <DayHeaderStyle BackColor="#666699" ForeColor="Maroon" />

            <DayStyle BackColor="#CCCCCC" Font-Bold="True" Font-Italic="False" ForeColor="#000066" />

            <SelectedDayStyle BackColor="#9999FF" Font-Bold="True" ForeColor="Maroon" />

            <WeekendDayStyle BackColor="#999966" Font-Bold="True" ForeColor="#990000" />

        </asp:Calendar>

        <asp:Label ID="LabelAction" runat="server"></asp:Label>

    </div>

    </form>

</body>

</html>

Step 2: Write the below code in cs page.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;

public partial class _Default : System.Web.UI.Page

{

    Hashtable HolidayList;

    protected void Page_Load(object sender, EventArgs e)

    {

        HolidayList = Getholiday();

        Calendar1.Caption = "Calender - Author: Pankaj Kumar Pandey";

        Calendar1.FirstDayOfWeek = FirstDayOfWeek.Monday;

        Calendar1.NextPrevFormat = NextPrevFormat.FullMonth;

        Calendar1.TitleFormat = TitleFormat.Month;

        Calendar1.ShowGridLines = true;

        Calendar1.DayStyle.Height = new Unit(50);

        Calendar1.DayStyle.Width = new Unit(150);

        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Center;

        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Middle;

 

        //Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.AliceBlue;

    }

    private Hashtable Getholiday()

    {

        Hashtable holiday = new Hashtable();

        holiday["1/1/2013"] = "New Year";

        holiday["1/5/2013"] = "Guru Govind Singh Jayanti";

        holiday["1/8/2013"] = "Muharam (Al Hijra)";

        holiday["1/14/2013"] = "Pongal";

        holiday["1/26/2013"] = "Republic Day";

        holiday["2/23/2013"] = "Maha Shivaratri";

        holiday["3/10/2013"] = "Milad un Nabi (Birthday of the Prophet";

        holiday["3/21/2013"] = "Holi";

        holiday["3/21/2013"] = "Telugu New Year";

        holiday["4/3/2013"] = "Ram Navmi";

        holiday["4/7/2013"] = "Mahavir Jayanti";

        holiday["4/10/2013"] = "Good Friday";

        holiday["4/12/2013"] = "Easter";

        holiday["4/14/2013"] = "Tamil New Year and Dr Ambedkar Birth Day";

        holiday["5/1/2013"] = "May Day";

        holiday["5/9/2013"] = "Buddha Jayanti and Buddha Purnima";

        holiday["6/24/2013"] = "Rath yatra";

        holiday["8/13/2013"] = "Krishna Jayanthi";

        holiday["8/14/2013"] = "Janmashtami";

        holiday["8/15/2013"] = "Independence Day";

        holiday["8/19/2013"] = "Parsi New Year";

        holiday["8/23/2013"] = "Vinayaka Chaturthi";

        holiday["9/2/2013"] = "Onam";

        holiday["9/5/2013"] = "Teachers Day";

        holiday["9/21/2013"] = "Ramzan";

        holiday["9/27/2013"] = "Ayutha Pooja";

        holiday["9/28/2013"] = "Vijaya Dasami (Dusherra)";

        holiday["10/2/2013"] = "Gandhi Jayanti";

        holiday["10/17/2013"] = "Diwali & Govardhan Puja";

        holiday["10/19/2013"] = "Bhaidooj";

        holiday["11/2/2013"] = "Guru Nanak Jayanti";

        holiday["11/14/2013"] = "Children's Day";

        holiday["11/28/2013"] = "Bakrid";

        holiday["12/25/2013"] = "Christmas";

        holiday["12/28/2013"] = "Muharram";

        return holiday;

    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)

    {

        if (HolidayList[e.Day.Date.ToShortDateString()] != null)

        {

            Literal literal1 = new Literal();

            literal1.Text = "<br/>";

            e.Cell.Controls.Add(literal1);

            Label label1 = new Label();

            label1.Text = (string)HolidayList[e.Day.Date.ToShortDateString()];

            label1.Font.Size = new FontUnit(FontSize.Small);

            e.Cell.Controls.Add(label1);

        }

    }

    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)

    {

        LabelAction.Text = "Month changed to :" + e.NewDate.ToShortDateString();

    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)

    {

        LabelAction.Text = "Date changed to :" + Calendar1.SelectedDate.ToShortDateString();

    }

}

 
Step 3: try it.

Ebook Download
View all
Learn
View all