Pages

Wednesday 31 October 2012

How to create a calendar showing holidays list of a year in Asp.Net?

Here, I am explaining how to create a calendar control showing holidays list of a year.
Step-1: Add a new page to your solution.
Step-2: Add a calendar server control and two labels into the page. Add styles to these controls as per your requirement.
Source View

<%@ 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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p style="text-align: center">
            <b></b>
     <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial Black"            
    Font-Size="Medium" ForeColor="#0066FF">Indian List of Holidays 2012</asp:Label>
        <br />           
        </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" >
            <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 />      
    </div>
    </form>
</body>
</html>

 Step-3: Write a method which will store all the holidays list in a HashTable and return the same.
private Hashtable Getholiday()
    {
        Hashtable holiday = new Hashtable();
        holiday["1/1/2012"] = "New Year";
        holiday["1/26/2012"] = "Republic Day";
        holiday["1/5/2012"] = "Guru Govind Singh Jayanti";
        holiday["1/8/2012"] = "Muharam (Al Hijra)";
        holiday["1/14/2012"] = "Pongal";
        holiday["1/26/2012"] = "Republic Day";
        holiday["2/23/2012"] = "Maha Shivaratri";
        holiday["3/10/2012"] = "Milad un Nabi (Birthday of the Prophet";
        holiday["3/21/2012"] = "Holi";
        holiday["3/21/2012"] = "Telugu New Year";
        holiday["4/3/2012"] = "Ram Navmi";
        holiday["4/7/2012"] = "Mahavir Jayanti";
        holiday["4/10/2012"] = "Good Friday";
        holiday["4/12/2012"] = "Easter";
        holiday["4/14/2012"] = "Tamil New Year and Dr Ambedkar Birth Day";
        holiday["5/1/2012"] = "May Day";
        holiday["5/9/2012"] = "Buddha Jayanti and Buddha Purnima";
        holiday["6/24/2012"] = "Rath yatra";
        holiday["8/13/2012"] = "Krishna Jayanthi";
        holiday["8/14/2012"] = "Janmashtami";
        holiday["8/15/2012"] = "Independence Day";
        holiday["8/19/2012"] = "Parsi New Year";
        holiday["8/23/2012"] = "Vinayaka Chaturthi";
        holiday["9/2/2012"] = "Onam";
        holiday["9/5/2012"] = "Teachers Day";
        holiday["9/21/2012"] = "Ramzan";
        holiday["9/27/2012"] = "Ayutha Pooja";
        holiday["9/28/2012"] = "Vijaya Dasami (Dusherra)";
        holiday["10/2/2012"] = "Gandhi Jayanti";
        holiday["10/17/2012"] = "Diwali & Govardhan Puja";
        holiday["10/19/2012"] = "Bhaidooj";
        holiday["11/2/2012"] = "Guru Nanak Jayanti";
        holiday["11/14/2012"] = "Children's Day";
        holiday["11/28/2012"] = "Bakrid";
        holiday["12/25/2012"] = "Christmas";
        holiday["12/28/2012"] = "Muharram";
        return holiday;
    }


Step-4: Declare a HashTable public and in the page_load, set it with the HashTable returned by the above method. Also add some formatting to the calendar control as followings.

    Hashtable HolidayList;
    protected void Page_Load(object sender, EventArgs e)
    {
        HolidayList = Getholiday();
        Calendar1.Caption = "Created By: Prajanuranjan Maharana";
        Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
        Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth;
        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;
    }

Step-5: Add OnDayRenderOnSelectionChanged and OnVisibleMonthChanged events of your Calendar Control. Write these following lines:

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        LabelAction.Text = "Date changed to :" + Calendar1.SelectedDate.ToShortDateString();
    }

    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
    {
        LabelAction.Text = "Month changed to :" + e.NewDate.ToShortDateString();
    }

    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);
        }
    }


Now, run your page and you will get your required output.
Output:



Regards,
Prajanuranjan....

Total Pageviews