Click here to Skip to main content
15,917,997 members
Articles / Web Development / ASP.NET

Calendar control and holidays

Rate me:
Please Sign up or sign in to vote.
3.58/5 (8 votes)
5 Jun 2008CPOL2 min read 66K   2.9K   31   6
Calendar control that distinguishes holidays and what day was chosen through the click event.


This article is a simple walkthrough of how to use the Calendar control in Visual Studio 2008 using the C# programming language. The Calendar control is a feature which allows the user to select dates specified or move in between months. There are many things you can customize under the Properties window in Visual Studio, such as DayHeaderStyle, DayStyle, NextPrevStyle, OtherMonthDayStyle, SelectedDayStyle, SelectorStyle, and WeedendDayStyle. These can be modified for a change in text, color, borders, alignment, width, and height. Since this is a simple basic walkthrough, we will leave those alone for now.

Using the Code

To start you off, you will want to run Visual Studio and create a new project. The ideal choice for this walkthrough would be to use a Web Form. Once your Web Project is created, you will notice your toolbox on the left of your screen. The first thing I will do is place a Label at the top of the page, identifying the page to the user. My Label will read “Calendar Selection Page”. I have also changed the format of the Label and the color of the page for aesthetics (of course, this is optional). Under your Standard tools, there is a control labeled Calendar. To insert this into your project, you may click and drag it into your design view screen, or double click the control in the toolbox.

Now, you should see a Calendar in your project window along with a Label. In this project, we want to acknowledge the selected date through a Label on the bottom portion of the screen. This is all determined through the properties you set with the SelectionMode in your properties window. By default, this property is set to Day, but you also have the options of DayWeek or DayWeekMonth. In this walkthrough, we are going to create a calendar that acknowledges which date you chose, by telling you the clicked upon date.

In order to make the date visible, once chosen, you will insert another Label directly under the Calendar control, with no Text property. I’m going to give my Label a name of selectedLabel. Now, let’s acknowledge a holiday through rendering the cell-size in the DayRender method. All of this is shown in the code below. This should give you a basic calendar that reads a date and acknowledges holidays within the Calendar control itself.

using System; 
using System.Collections; 
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; 

namespace calenderControl
public partial class _Default : System.Web.UI.Page 
    string[,] holiDay = new string[13, 32]; 
    protected void Page_Load(object sender, EventArgs e) 
        holiDay[1, 1] = "New Years Day"; 
        holiDay[1, 17] = "Martin Luther King Day"; 
        holiDay[2, 1] = "National Freedom Day"; 
        holiDay[2, 14] = "Valentine's Day"; 
            holiDay[2, 21] = "Presidents' Day"; 
            holiDay[3, 27] = "Easter Sunday"; 
        holiDay[4, 15] = "Tax Day"; 
        holiDay[5, 1] = "Loyalty Day"; 
        holiDay[5, 8] = "Mothers' Day"; 
            holiDay[5, 22] = "National Meritime Day"; 
        holiDay[5, 30] = "Memorial Day"; 
        holiDay[6, 19] = "Fathers' Day"; 
            holiDay[7, 4] = "Independence Day"; 
        holiDay[9, 5] = "Labor day"; 
        holiDay[9, 11] = "Patriot day"; 
        holiDay[10, 10] = "Columbus Day"; 
        holiDay[10, 31] = "Halloween"; 
        holiDay[11, 11] = "Veterans Day"; 
        holiDay[11, 24] = "Thanksgiving Day"; 
        holiDay[12, 24] = "Christmas Eve Day"; 
        holiDay[12, 25] = "Christmas Day"; 

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) 
        CalendarDay day = (CalendarDay)e.Day; 
        TableCell cell = (TableCell)e.Cell; 

        if (!day.IsOtherMonth) 
            String holidayStr = holiDay[day.Date.Month, day.Date.Day]; 

            if (holidayStr != null) 
                cell.BackColor = System.Drawing.Color.HotPink; 
                cell.Controls.Add(new LiteralControl("
                " + holidayStr)); 

    protected void Calendar1_SelectionChanged(object sender, EventArgs e) 
        selectedLabel.Text = "Date Chosen: " + 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionMy Vote of 3 Pin
Alireza_136217-Dec-12 15:30
Alireza_136217-Dec-12 15:30 
QuestionHow to change the Tooltip link on MouseOver when rendering a day? [modified] Pin
pcguerra24-Aug-10 10:55
pcguerra24-Aug-10 10:55 
Generalit doesn't work Pin
remandlo@gmail.com5-Mar-09 1:09
remandlo@gmail.com5-Mar-09 1:09 
GeneralI dont get it Pin
Ilan Perez1-Oct-08 2:53
Ilan Perez1-Oct-08 2:53 
GeneralAlright Article Pin
marinaccio6-Jun-08 18:00
marinaccio6-Jun-08 18:00 
GeneralNice simple tutorial. Pin
Rajib Ahmed5-Jun-08 16:57
Rajib Ahmed5-Jun-08 16:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.