Export (0) Print
Expand All

Calendar Class

Displays a single-month calendar that allows the user to select dates and move to the next or previous month.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[ControlValuePropertyAttribute("SelectedDate", typeof(DateTime), "1/1/0001")]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Calendar : WebControl, IPostBackEventHandler
<asp:Calendar />

Use the Calendar control to display a single month of a calendar on a Web page. The control allows you to select dates and move to the next or previous month. The Calendar control supports all the System.Globalization.Calendar types in the System.Globalization namespace. Apart from the Gregorian calendar, this also includes calendars that use different year and month systems, such as the Hjiri calendar.

You can specify whether the Calendar control allows you to select a single day, week, or entire month by setting the SelectionMode property.

By default, the control displays the days of the month, day headings for the days of the week, a title with the month name and year, links for selecting individual days of the month, and links for moving to the next and previous month. You can customize the appearance of the Calendar control by setting the properties that control the style for different parts of the control. The following table lists the properties that specify the style for the different parts of the control.

Property

Description

DayHeaderStyle

Specifies the style for the section that displays the days of the week.

DayStyle

Specifies the style for the dates in the displayed month.

NextPrevStyle

Specifies the style for the navigation controls in the title section.

OtherMonthDayStyle

Specifies the style for the dates that are not in the currently displayed month.

SelectedDayStyle

Specifies the style for the selected dates on the calendar.

SelectorStyle

Specifies the style for the week and month date-selection column.

TitleStyle

Specifies the style for the title section.

TodayDayStyle

Specifies the style for today's date.

WeekendDayStyle

Specifies the style for the weekend dates.

You can also show or hide different parts of the control. The following table lists the properties that control which parts are shown or hidden.

Property

Description

ShowDayHeader

Shows or hides the section that displays the days of the week.

ShowGridLines

Shows or hides the gridlines between the days of the month.

ShowNextPrevMonth

Shows or hides the navigation controls to the next or previous month.

ShowTitle

Shows or hides the title section.

Although binding to a data source is not supported for the Calendar control, you can modify the content and formatting of the individual date cells. Before the Calendar control is displayed on the Web page, it creates and assembles the components that make up the control. The DayRender event is raised when each date cell in the Calendar control is created. You can control the contents and formatting of a date cell as it is created by providing code in the event handler for the DayRender event. For more information on customizing the contents of a date cell, see OnDayRender.

NoteNote:

The Calendar control renders ECMAScript (JScript, JavaScript) to the client browser. The client browser must have ECMAScript enabled for this control to function properly. For more information on client script, see Client Script in ASP.NET Web Pages.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

TopicLocation
How to: Format Calendar Web Server Control Elements Using StylesBuilding ASP .NET Web Applications
How to: Control User Date Selection in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Respond to Date Selection in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Customize Individual Days in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Set Today's Date Programmatically in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Display Selected Dates from a Database in the Calendar ControlBuilding ASP .NET Web Applications
How to: Control Month Navigation in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Read Selected Dates in the Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Customize Calendar Web Server Control AppearanceBuilding ASP .NET Web Applications
How to: Format Calendar Web Server Control Elements Using StylesBuilding ASP .NET Web Applications
How to: Control User Date Selection in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Respond to Date Selection in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Customize Individual Days in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Set Today's Date Programmatically in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Display Selected Dates from a Database in the Calendar ControlBuilding ASP .NET Web Applications
How to: Control Month Navigation in a Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Read Selected Dates in the Calendar Web Server ControlBuilding ASP .NET Web Applications
How to: Customize Calendar Web Server Control AppearanceBuilding ASP .NET Web Applications
Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Data Binding to a Custom Business ObjectBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Basic Web Page in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Customizing a Web Site Using Themes in Visual StudioBuilding ASP .NET Web Applications in Visual Studio
How to: Format Calendar Web Server Control Elements Using Styles

The following code example demonstrates how to create a Calendar control on a Web page.

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>

   <form id="form1" runat="server">

      <asp:Calendar id="calendar1" runat="server">

           <OtherMonthDayStyle ForeColor="LightGray">
           </OtherMonthDayStyle>

           <TitleStyle BackColor="Blue"
                       ForeColor="White">
           </TitleStyle>

           <DayStyle BackColor="gray">
           </DayStyle>

           <SelectedDayStyle BackColor="LightGray"
                             Font-Bold="True">
           </SelectedDayStyle>

      </asp:Calendar>

   </form>

</body>
</html>

The following example shows a Calendar control used when editing one of the columns displayed by a GridView control. The Calendar control is bound to the data source with the Bind method. The Eval method is used to bind the date value that is displayed by the Calendar control.

<Columns>                  
  <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>                    
  <asp:BoundField DataField="FirstName"  HeaderText="First Name"/>
  <asp:BoundField DataField="LastName"   HeaderText="Last Name"/>                    
  <asp:TemplateField HeaderText="Birth Date">
    <ItemTemplate> 
      <asp:Label ID="BirthDateLabel" Runat="Server" 
                 Text='<%# Eval("BirthDate", "{0:d}") %>' />
    </ItemTemplate>
    <EditItemTemplate>
      <asp:Calendar ID="EditBirthDateCalendar" Runat="Server"
                    VisibleDate='<%# Eval("BirthDate") %>'
                    SelectedDate='<%# Bind("BirthDate") %>' />
    </EditItemTemplate>
  </asp:TemplateField> 
  <asp:HyperLinkField Text="Show Detail"
                      DataNavigateUrlFormatString="~/ShowEmployeeDetail.aspx?EmployeeID={0}"
                      DataNavigateUrlFields="EmployeeID" />                   
</Columns> 

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft