Calendar Control in ASP.NET
In this article we are reading about “Calendar control in ASP.NET”.
The calendar control is a functionally rich web control, which provides the following capabilities:
- Displaying one month at a time
- Selecting a day, a week or a month
- Selecting a range of days
- Moving from month to month
- Controlling the display of the days programmatically
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
|
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.aspx code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Calendar.aspx.cs" Inherits="_Calendar" %>
<form id="form1"> <div> <p style="text-align: center;"><b></b> Indian List of Holidays 2009</p> <b></b> </div> </form> |
Calendar.aspx.cs
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System;
using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections; public partial class _Default : System.Web.UI.Page { Hashtable HolidayList; protected void Page_Load(object sender, EventArgs e) { HolidayList = Getholiday(); Cal1.FirstDayOfWeek = FirstDayOfWeek.Sunday; Cal1.NextPrevFormat = NextPrevFormat.ShortMonth; Cal1.TitleFormat = TitleFormat.Month; Cal1.ShowGridLines = true; Cal1.DayStyle.Height = new Unit(50); Cal1.DayStyle.Width = new Unit(150); Cal1.DayStyle.HorizontalAlign = HorizontalAlign.Center; Cal1.DayStyle.VerticalAlign = VerticalAlign.Middle; Cal1.OtherMonthDayStyle.BackColor = System.Drawing.Color.AliceBlue; } private Hashtable Getholiday() { Hashtable holiday = new Hashtable(); holiday["1/1/2009"] = "New Year"; holiday["1/5/2009"] = "Guru Govind Singh Jayanti"; holiday["1/8/2009"] = "Muharam (Al Hijra)"; holiday["1/14/2009"] = "Pongal"; holiday["1/26/2009"] = "Republic Day"; holiday["2/23/2009"] = "Maha Shivaratri"; holiday["3/10/2009"] = "Milad un Nabi (Birthday of the Prophet"; holiday["3/21/2009"] = "Holi"; holiday["3/21/2009"] = "Telugu New Year"; holiday["4/3/2009"] = "Ram Navmi"; holiday["4/7/2009"] = "Mahavir Jayanti"; holiday["4/10/2009"] = "Good Friday"; holiday["4/12/2009"] = "Easter"; holiday["4/14/2009"] = "Tamil New Year and Dr Ambedkar Birth Day"; holiday["5/1/2009"] = "May Day"; holiday["5/9/2009"] = "Buddha Jayanti and Buddha Purnima"; holiday["6/24/2009"] = "Rath yatra"; holiday["8/13/2009"] = "Krishna Jayanthi"; holiday["8/14/2009"] = "Janmashtami"; holiday["8/15/2009"] = "Independence Day"; holiday["8/19/2009"] = "Parsi New Year"; holiday["8/23/2009"] = "Vinayaka Chaturthi"; holiday["9/2/2009"] = "Onam"; holiday["9/5/2009"] = "Teachers Day"; holiday["9/21/2009"] = "Ramzan"; holiday["9/27/2009"] = "Ayutha Pooja"; holiday["9/28/2009"] = "Vijaya Dasami (Dusherra)"; holiday["10/2/2009"] = "Gandhi Jayanti"; holiday["10/17/2009"] = "Diwali & Govardhan Puja"; holiday["10/19/2009"] = "Bhaidooj"; holiday["11/2/2009"] = "Guru Nanak Jayanti"; holiday["11/14/2009"] = "Children's Day"; holiday["11/28/2009"] = "Bakrid"; holiday["12/25/2009"] = "Christmas"; holiday["12/28/2009"] = "Muharram"; return holiday; } protected void Cal1_SelectionChanged(object sender, EventArgs e) { LabelAction.Text = "Date changed to :" + Cal1.SelectedDate.ToShortDateString(); } protected void Cal1_VisibleMonthChanged(object sender, MonthChangedEventArgs e) { LabelAction.Text = "Month changed to :" + e.NewDate.ToShortDateString(); } protected void Cal1_DayRender(object sender, DayRenderEventArgs e) { if (HolidayList[e.Day.Date.ToShortDateString()] != null) { Literal literal1 = new Literal(); literal1.Text = " "; 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); } } } |
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 Cal1_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

So in this article we have read about “Calendar control in ASP.NET”.
Comments
Post a Comment