New Document
Calendar Tutorial for ASP.NET

Calendar is an ASP.NET AJAX extender that can be attached to any ASP.NET TextBox control. It provides client-side date-picking functionality with customizable date format and UI in a popup control. You can interact with the calendar by clicking on a day to set the date, or the "Today" link to set the current date.

In addition, the left and right arrows can be used to move forward or back a month. By clicking on the title of the calendar you can change the view from Days in the current month, to Months in the current year. Another click will switch to Years in the current Decade. This action allows you to easily jump to dates in the past or the future from within the calendar control.

The page uses the culture setting English (United States) which was specified by the browser. The page properties have Culture="auto" and UICulture="auto" set to enable the same. See this MSDN article for more information. The ScriptManager on this Calendar demo page has EnableScriptGlobalization="true" and EnableScriptLocalization="true".

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

The basic syntax for adding a calendar control is:

<asp:Calender ID = "Calendar1" runat = "server"></asp:Calender>
Properties and Events of the Calendar Control

The calendar control has many properties and events, using which you can customize the actions and display of the control. The following table provides some important properties of the Calendar control:

CaptionGets or sets the caption for the calendar control.
CaptionAlignGets or sets the alignment for the caption.
CellPaddingGets or sets the number of space between the data and the cell.s border.
CellSpacingGets or sets the space between cells.
DayHeaderStyleGets the style properties for the section that displays the day of the week.
DayNameFormatGets or sets format of days of the week.
DayStyleGets the style properties for the days in the displayed month.
FirstDayOfWeekGets or sets the day of week to display in the first column.
NextMonthTextGets or sets the text for next month navigation control; default value is >
NextPrevFormatGets or sets the format of the next and previous month navigation control.
OtherMonthDayStyleGets the style properties for the days on the Calendar control that are not in the displayed month.
PrevMonthTextGets or sets the text for previous month navigation control; default value is <
SelectedDateGets or sets the selected date.
SelectedDatesGets a collection of DateTime objects representing the selected dates.
SelectedDayStyleGets the style properties for the selected dates.
SelectionModeGets or sets the selection mode that specifies whether the user can select a single day, a week or an entire month.
SelectMonthTextGets or sets the text for the month selection element in the selector column.
SelectorStyleGets the style properties for the week and month selector column.
SelectWeekTextGets or sets the text displayed for the week selection element in the selector column.
ShowDayHeaderGets or sets the value indicating whether the heading for the days of the week is displayed.
ShowGridLinesGets or sets the value indicating whether the gridlines would be shown.
ShowNextPrevMonthGets or sets a value indicating whether next and previous month navigation elements are shown in the title section.
ShowTitleGets or sets a value indicating whether the title section is displayed.
TitleFormatGets or sets the format for the title section.
TitlestyleGet the style properties of the title heading for the Calendar control.
TodayDayStyleGets the style properties for today's date on the Calendar control.
TodaysDateGets or sets the value for today.s date.
UseAccessibleHeaderGets or sets a value that indicates whether to render the table header <th> HTML element for the day headers instead of the table data <td> HTML element.
VisibleDateGets or sets the date that specifies the month to display.
WeekendDayStyleGets the style properties for the weekend dates on the Calendar control.

The Calendar control has the following three most important events that allow the developers to program the calendar control. These are:

SelectionChanged It is raised when a day, a week or an entire month is selected
DayRender It is raised when each data cell of the calendar control is rendered.
VisibleMonthChanged It is raised when user changes a month
Working with the Calendar Control

Putting a bare-bone calendar control without any code behind file provides a workable calendar to a site, which shows the month and days of the year. It also allows navigating to next and previous months.


Calendar controls allow the users to select a single day, a week or an entire month. This is done by using the SelectionMode property. This property has the following values:

Day To select a single day
DayWeek To select a single day or an entire week
DayWeekMonth To select a single day, a week or an entire month
None Nothing can be selected

The syntax for selecting days:

<asp:Calender ID = "Calendar1" 
                 runat = "server"

When the selection mode is set to the value DayWeekMonth, an extra column with the > symbol appears for selecting the week and a >> symbol appears to the left of the days' name for selecting the month.


The following example demonstrates selecting a date and displays the date in a label:

The content file:

<%@ Page Language="C#" 
         Inherits="calendardemo._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <h3> Your Birthday:</h3>
        <asp:Calendar ID="Calendar1" 
    <p>Todays date is: 
        <asp:Label ID="lblday" runat="server"></asp:Label>
    <p>Your Birday is: 
        <asp:Label ID="lblbday" runat="server"></asp:Label>

The event handler for the event SelectionChanged:

protected void Calendar1_SelectionChanged(object sender, 
                                          EventArgs e)
    lblday.Text = Calendar1.TodaysDate.ToShortDateString();
    lblbday.Text = Calendar1.SelectedDate.ToShortDateString();

When the file run should produce the following output:


Previous                                                                                                                                                       Next

Back to Top