C# - The Calendar Class

The Calendar class is under System.Web.UI namespace.

By placing a simple tag in a Web form, you can render an HTML-based calendar that can accept user input.

The Calendar Web control is instantiated like this:

<asp:Calendar id="MyCalendar" runat="server"></asp:Calendar>

Properties of the Calendar Class

Item Description
CellPadding Specifies the padding distance inside cells in the Calendar control
CellSpacing Specifies the amount of spacing between cells in the Calendar control
DayHeaderStyle Specifies style and format information for the day headers
DayNameFormat Specifies the format of the day names
DayStyle Specifies style and format information for the days in the month
FirstDayOfWeek Specifies the first calendar day of the week
NextMonthText Specifies the text to display in the hyperlink to show next month
NextPrevFormat Specifies the format of the next and previous month navigation elements
NextPrevStyle Specifies style and format information tp apply to the next and previous month navigation elements
OtherMonthDayStyle Specifies style and format information for days on the calendar that are not in the current month
PrevMonthText Spcifies the text of the hyperlink which shows the previous month
SelectedDate Specifies the currently selected date
SelectedDayStyle Specifies the style and format of the day the user has selected
SelectionMode Specifies the selection choices a user has available
SelectMonthText Specifies the text of the hyperlink that enables a user to select an entire month
SelectorStyle Contains the style and format information for the week and month selection elements
SelectWeekText Specifies the text to be displayed next to the weeks that will enable a user to select a week at a time
ShowDayHeader Specifies whether the header row on the Calendar control should be displayed
ShowGridLines Specifies whether the Calendar grid lines should be rendered
ShowNextPrevMonth Specifies whether the next and previous month links should appear
ShowTitle Specifies whether the Calendar title is rendered
TitleFormat Specifies the format of the title
TitleStyle Contains style and format information for the Calendar title
TodayDayStyle Contains style and format information for the current day
TodaysDate Specifies the current date
VisibleDate Specifies the date used to determine which month to display on the rendered calendar
WeekendDayStyle Contains style and format information for the Weekend day
Calendar.CellPadding

Syntax

Int32 CellPadding

Description

The Calendar.CellPadding property changes the padding distance between cells in the Calendar control.

Example

MyCalendar.CellPadding = 4;
Calendar.CellSpacing

Syntax

Int32 CellSpacing

Description

The Calendar.CellSpacing property specifies the amount of spacing between cells in the Calendar control.

Example

MyCalendar.CellSpacing = 4;
Calendar.DayHeaderStyle

Syntax

TableItemStyle DayHeaderStyle

Description

The Calendar.DayHeaderStyle property specifies style and format information for the day headers at the top of the Calendar control.

Example

MyCalendar.DayHeaderStyle.CssClass = "main";
Calendar.DayNameFormat

Syntax

DayNameFormat DayNameFormat

Description

The Calendar.DayNameFormat property specifies the format of the day names as rendered by the Calendar control. Valid values are FirstLetter, FirstTwoLetters, Full, and Short. Short is the default value.

Example

MyCalendar.DayNameFormat = DayNameFormat.Full;
Calendar.DayStyle

Syntax

TableItemStyle DayStyle

Description

The Calendar.DayStyle property specifies style and format information for the days in the month.

Example

MyCalendar.DayStyle.CssClass = "main";
Calendar.FirstDayOfWeek

Syntax

FirstDayOfWeek FirstDayOfWeek

Description

The Calendar.FirstDayOfWeek property specifies the first calendar day of the week. Sunday is the default first day of the week.

Example

MyCalendar.FirstDayOfWeek = FirstDayOfWeek.Wednesday;
Calendar.NextMonthText

Syntax

String NextMonthText

Description

The Calendar.NextMonthText property specifies the text to display in the header of the Calendar control that directs the user to click to the next month. By default, this property is blank.

Example

MyCalendar.NextMonthText = "Next Month>";
Calendar.NextPrevFormat

Syntax

NextPrevFormat NextPrevFormat

Description

The Calendar.NextPrevFormat property specifies the format of the next and previous month navigation elements. Valid values are CustomText, FullMonth, and ShortMonth. FullMonth displays the full month name and ShortMonth displays an abbreviation.

Example

MyCalendar.NextPrevFormat = FullMonth;
Calendar.NextPrevStyle

Syntax

TableItemStyle NextPrevStyle

Description

The Calendar.NextPrevStyle property specifies style and format information to apply to the next and previous month navigation elements.

Example

MyCalendar.NextPrevStyle.CssClass = "header";
Calendar.OtherMonthDayStyle

Syntax

TableItemStyle OtherMonthDayStyle

Description

The Calendar.OtherMonthDayStyle property specifies style and format information for days on the calendar that are not in the current month.

Example

MyCalendar.OtherMonthDayStyle.CssClass = "OtherDay";
Calendar.PrevMonthText

Syntax

String PrevMonthText

Description

The Calendar.PrevMonthText property specifies the text to display in the header of the Calendar control that directs the user to click to the previous month. By default, this property is blank.

Example

MyCalendar.NextMonthText = "<Previous Month";
Calendar.SelectedDate

Syntax

DateTime SelectedDate

Description

The Calendar.SelectedDate property contains the currently selected date.

Example

DateTime MyDate = MyCalendar.SelectedDate;
Calendar.SelectedDayStyle

Syntax

TableItemStyle SelectedDateStyle

Description

The Calendar.SelectedDayStyle property specifies the style and format of the day the user has selected.

Example

MyCalendar.SelectedDayStyle.CssClass = "SelectedDay";
Calendar.SelectionMode

Syntax

CalendarSelectionMode SelectionMode

Description

The Calendar.SelectionMode property specifies the selection choices a user has available. The user can select the day, week, or month, based on the SelectionMode property. Valid values are Day, DayWeek, DayWeekMonth, and None.

Example

MyCalendar.SelectionMode = CalendarSelectionMode.Day;
Calendar.SelectMonthText

Syntax

String SelectMonthText

Description

The Calendar.SelectMonthText property enables you to specify text that will appear in the upper-left corner of the rendered calendar. When a user clicks on this text, the entire month will be selected. In order for this property to function, the Calendar.SelectionMode must be set to CalendarSelectionMode.DayWeekMonth.

Example

MyCalendar.SelectionMode = CalendarSelectionMode.DayWeekMonth;
MyCalendar.SelectMonthText = "select month";
Calendar.SelectorStyle

Syntax

TableItemStyle SelectorStyle

Description

The Calendar.SelectorStyle property contains the style and format information for the week and month selection elements.

Example

string cssClass = MyCalendar.SelectorStyle.CssClass;
Calendar.SelectWeekText

Syntax

String SelectWeekText

Description

The Calendar.SelectWeekText property specifies the text to be displayed next to the weeks that will enable a user to select those weeks.

Example

MyCalendar.SelectWeekText = ">>";
Calendar.ShowDayHeader

Syntax

Boolean ShowDayHeader

Description

The Calendar.ShowDayHeader property specifies whether the header row on the Calendar control should be displayed. The default is True.

Example

MyCalendar.ShowDayHeader = false;
Calendar.ShowGridLines

Syntax

Boolean ShowGridLines

Description

The Calendar.ShowGridLines property specifies whether the calendar grid lines should be rendered. The default is True.

Example

MyCalendar.ShowGridLines = false;
Calendar.ShowNextPrevMonth

Syntax

Boolean ShowNextPrevMonth

Description

The Calendar.ShowNextPrevMonth property specifies whether the next and previous month links should appear. The default is True.

Example

MyCalendar.ShowNextPrevMonth = false;
Calendar.ShowTitle

Syntax

Boolean ShowTitle

Description

The Calendar.ShowTitle property specifies whether the calendar title is rendered. The default is True.

Example

MyCalendar.ShowTitle = false;
Calendar.TitleFormat

Syntax

TitleFormat TitleFormat

Description

The Calendar.TitleFormat property specifies the format of the title. Valid values are Month and MonthYear. The default is MonthYear.

Example

MyCalendar.TitleFormat = TitleFormat.Month;
Calendar.TitleStyle

Syntax

TableItemStyle TitleStyle

Description

The Calendar.TitleStyle property contains the style and format information for the calendar's title.

Example

string MyClass = MyCalendar.TitleStyle.CssClass;
Calendar.TodayDayStyle

Syntax

TableItemStyle TodayDayStyle

Description

The Calendar.TodayDayStyle property contains the style and format information for the current day in the calendar.

Example

string MyClass = MyCalendar.TodayDayStyle.CssClass;
Calendar.TodaysDate

Syntax

DateTime TodaysDate

Description

The Calendar.TodaysDate property specifies the current date.

Example

DateTime MyDate = MyCalendar.TodaysDate;
Calendar.VisibleDate

Syntax

DateTime VisibleDate

Description

The Calendar.VisibleDate property specifies the date used to determine which month to display in the rendered calendar.

Example

MyCalendar.VisibleDate = new DateTime(2006, 12, 1);
Calendar.WeekendDayStyle

Syntax

TableItemStyle WeekendDayStyle

Description

The Calendar.WeekendDayStyle property contains the style and format information of weekend days in the rendered calendar.

Example

string MyClass = MyCalendar.WeekendDayStyle.CssClass;