Calendar control in ASP.NET

Birthdays, anniversaries, appointments, holidays, bill payments, and project deadlines. All these have one thing in common. Guess? It's a date. It is difficult to remember dates so for that calendar comes to your rescue.
 
ASP.NET provides a Calendar control that is used to display a calendar on the Web page.
 
This control displays a one-month calendar that allows the user to select dates and move to the next and previous months.
 
By default, this control displays the name of the current month, day headings for the days of the weeks, days of the month and arrow characters for navigation to the previous or next month.
 
The class hierarchy for this control is as follows:
 
Object -> Control -> WebControl -> Calendar
 
The Calendar is complex, powerful Web server control that you can use to add calendar feature to your web page. We can use calendar control display any date between 0 A.D. and 9999A.D.
 
The Calendar control is represented as:
  1. <asp:Calendar ID="Calendar1" runat="server" </asp:Calendar> 
element in Source view.
 
The Calendar control when rendered to a user browser, it generates an HTML <table> element and a set of associated JavaScript.
 
The Calender control can be used to select a single date or multiple dates. The SelectionMode property is used for this.
 
The SelectionMode properties are as: 
 
Property Description
Day Allow selection of a single date.
DayWeek Allows the selection of a single date or a complete week.
DayWeekMonth
Allow selection of single date, complete week or complete month.
None Doesn't allow you to select any date.
 
You can also set the properties for the calendar either by selecting the Auto Format property by right clicking on the Calendar control or by manually setting them one by one.
 
Calendar Properties
 
There are many properties of Calendar control to customize the functionality and appearance. These can be read at msdn.
 
Let us start with a basic example that will help you in making a small application showing list of Indian holidays.
 
calendar control 
 
Default.aspx code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title>Untitled Page</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <p style="text-align: center">  
  11.         <b></b>  
  12.         <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial Black" Font-Size="Medium"  
  13.             ForeColor="#0066FF">Indian List of Holidays 2009</asp:Label><br /></b>  
  14.         </p>  
  15.         <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"  
  16.             BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"  
  17.             ForeColor="#663399" ShowGridLines="True" OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged"  
  18.             OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">  
  19.             <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />  
  20.             <SelectorStyle BackColor="#FFCC66" />  
  21.             <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />  
  22.             <OtherMonthDayStyle ForeColor="#CC9966" />  
  23.             <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />  
  24.             <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />  
  25.             <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />  
  26.         </asp:Calendar>  
  27.         <br />  
  28.         <b></b>  
  29.         <asp:Label ID="LabelAction" runat="server"></asp:Label><br />  
  30.         </b>  
  31.     </div>  
  32.     </form>  
  33. </body>  
  34. </html>
Code behind file
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Collections;  
  13.    
  14. public partial class _Default : System.Web.UI.Page  
  15. {  
  16.     Hashtable HolidayList;  
  17.    
  18.     protected void Page_Load(object sender, EventArgs e)  
  19.     {  
  20.         HolidayList = Getholiday();  
  21.         Calendar1.Caption = "Calender - Author: Puran Singh Mehra";  
  22.         Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;  
  23.         Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth;  
  24.         Calendar1.TitleFormat = TitleFormat.Month;  
  25.         Calendar1.ShowGridLines = true;  
  26.         Calendar1.DayStyle.Height = new Unit(50);  
  27.         Calendar1.DayStyle.Width = new Unit(150);  
  28.         Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Center;  
  29.         Calendar1.DayStyle.VerticalAlign = VerticalAlign.Middle;  
  30.         Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.AliceBlue;  
  31.     }  
  32.   
  33.     private Hashtable Getholiday()  
  34.     {         
  35.         Hashtable holiday = new Hashtable();  
  36.         holiday["1/1/2009"] = "New Year";  
  37.         holiday["1/5/2009"] = "Guru Govind Singh Jayanti";  
  38.         holiday["1/8/2009"] = "Muharam (Al Hijra)";  
  39.         holiday["1/14/2009"] = "Pongal";  
  40.         holiday["1/26/2009"] = "Republic Day";  
  41.         holiday["2/23/2009"] = "Maha Shivaratri";  
  42.         holiday["3/10/2009"] = "Milad un Nabi (Birthday of the Prophet";  
  43.         holiday["3/21/2009"] = "Holi";  
  44.         holiday["3/21/2009"] = "Telugu New Year";  
  45.         holiday["4/3/2009"] = "Ram Navmi";  
  46.         holiday["4/7/2009"] = "Mahavir Jayanti";  
  47.         holiday["4/10/2009"] = "Good Friday";  
  48.         holiday["4/12/2009"] = "Easter";  
  49.         holiday["4/14/2009"] = "Tamil New Year and Dr Ambedkar Birth Day";  
  50.         holiday["5/1/2009"] = "May Day";  
  51.         holiday["5/9/2009"] = "Buddha Jayanti and Buddha Purnima";  
  52.         holiday["6/24/2009"] = "Rath yatra";  
  53.         holiday["8/13/2009"] = "Krishna Jayanthi";  
  54.         holiday["8/14/2009"] = "Janmashtami";  
  55.         holiday["8/15/2009"] = "Independence Day";  
  56.         holiday["8/19/2009"] = "Parsi New Year";  
  57.         holiday["8/23/2009"] = "Vinayaka Chaturthi";  
  58.         holiday["9/2/2009"] = "Onam";  
  59.         holiday["9/5/2009"] = "Teachers Day";  
  60.         holiday["9/21/2009"] = "Ramzan";  
  61.         holiday["9/27/2009"] = "Ayutha Pooja";  
  62.         holiday["9/28/2009"] = "Vijaya Dasami (Dusherra)";  
  63.         holiday["10/2/2009"] = "Gandhi Jayanti";  
  64.         holiday["10/17/2009"] = "Diwali & Govardhan Puja";  
  65.         holiday["10/19/2009"] = "Bhaidooj";  
  66.         holiday["11/2/2009"] = "Guru Nanak Jayanti";  
  67.         holiday["11/14/2009"] = "Children's Day";  
  68.         holiday["11/28/2009"] = "Bakrid";  
  69.         holiday["12/25/2009"] = "Christmas";  
  70.         holiday["12/28/2009"] = "Muharram";  
  71.         return holiday;  
  72.     }  
  73.   
  74.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)  
  75.     {  
  76.         LabelAction.Text = "Date changed to :" + Calendar1.SelectedDate.ToShortDateString();  
  77.     }  
  78.   
  79.     protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)  
  80.     {  
  81.         LabelAction.Text = "Month changed to :" + e.NewDate.ToShortDateString();  
  82.     }  
  83.   
  84.     protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)  
  85.     {  
  86.         if (HolidayList[e.Day.Date.ToShortDateString()] != null)  
  87.         {  
  88.             Literal literal1 = new Literal();  
  89.             literal1.Text = "<br/>";  
  90.             e.Cell.Controls.Add(literal1);  
  91.             Label label1 = new Label();  
  92.             label1.Text = (string)HolidayList[e.Day.Date.ToShortDateString()];  
  93.             label1.Font.Size = new FontUnit(FontSize.Small);  
  94.             e.Cell.Controls.Add(label1);  
  95.         }  
  96.     }  
  97. }  
Code review
 
In the above code we have used DayRender, SelectionChanged and VisibleMonthChanged events. A method called Getholiday() is added to populate a collection of holidays. In the Calendar1_DayRender event handler we are displaying the holiday in a label, which is created, and is added to the Cell objects Controls collection.
 
Output of the above code
 
calender control 
 
Conclusion
 
I hope that this article would have helped you in understanding the calendar control. Certainly there are a lot of variants how calendar control can be designed, but my aim was to provide just a basic tutorial on writing and using Calendar controls.
 
Please share it if you know more about this article. Your feedback and constructive contributions are welcome.