Click here to Skip to main content
6,295,667 members and growing! (12,573 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate License: The Code Project Open License (CPOL)

Calendar control and holidays

By NathanielSuho

Calendar control that distinguishes holidays and what day was chosen through the click event.
C#, Windows, .NET, ASP.NET, Dev
Posted:5 Jun 2008
Views:11,504
Bookmarked:24 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
7 votes for this article.
Popularity: 3.00 Rating: 3.55 out of 5

1
2 votes, 28.6%
2
1 vote, 14.3%
3
2 votes, 28.6%
4
2 votes, 28.6%
5

Introduction

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: " + 
            Calendar1.SelectedDate.ToLongDateString(); 
    } 
} 
}

License

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

About the Author

NathanielSuho


Member

Occupation: Other
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Generalit doesn't work Pinmemberremandlo2:09 5 Mar '09  
GeneralI dont get it PinmemberIlan Perez3:53 1 Oct '08  
GeneralAlright Article Pinmembermarinaccio19:00 6 Jun '08  
GeneralNice simple tutorial. PinmemberRajib Ahmed17:57 5 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jun 2008
Editor: Smitha Vijayan
Copyright 2008 by NathanielSuho
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project