asp.net.ph

Skip Navigation Links

Calendar Control Syntax

ASP.NET Syntax   ASP.NET Syntax for Web Controls


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

Declarative Syntax

For information on the individual members of this class, see Calendar in the class library.

Remarks

The Calendar control displays a one-month calendar that allows the user to select dates and move to the next and previous months.

By setting the SelectionMode property, you can specify whether the user can select a single day, a week, or a month, or you can disable date selection entirely.

Setting the style properties for the different parts of the control customizes the appearance of the Calendar control. The following table lists the different style properties for the Calendar control.

Style object Description Style class
DayHeaderStyle The style for the section above the calendar where the names of the days appear. TableItemStyle
DayStyle The style for the days in the displayed month.

NOTE: Weekends, the current date, and the selected day can have different styles by setting the WeekendDayStyle, TodayDayStyle, and SelectedDayStyle properties, respectively.

TableItemStyle
NextPrevStyle The style for the sections at the left and right ends of the title bar where the month navigation LinkButton controls are located. TableItemStyle
OtherMonthDayStyle The style for the days from the previous and next month that appear on the current month view. TableItemStyle
SelectedDayStyle The style for the selected date.

NOTE: If this property is not set, the style specified by the DayStyle property is used to display the selected date.

TableItemStyle
SelectorStyle The style for the column on the left of the Calendar control containing links for selecting a week or the entire month. TableItemStyle
TitleStyle The style for the title bar at the top of the calendar containing the month name and month navigation links.

NOTE: If NextPrevStyle is set, it overrides the style for the next and previous month navigation controls located at the ends of the title bar.

TableItemStyle
TodayDayStyle The style for the current date.

NOTE: If this property is not set, the style specified by the DayStyle property is used to display the current date.

TableItemStyle
WeekendDayStyle The style for the weekend days.

NOTE: If this property is not set, the style specified by the DayStyle property is used to display the weekend dates.

TableItemStyle

You also control the appearance of Calendar control by showing or hiding different parts of the control. The following table lists the different parts of the Calendar control that can be shown or hidden.

Property Description
ShowDayHeader Shows or hides the section that displays the days of the week.
ShowGridLines Shows or hides the grid lines 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 in 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 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 information about the properties supported for each style class, see Style Object Properties.

Syntax Example

The below code snippet shows a sample declaration for a Calendar control in an .aspx file. The declaration includes a number of style object properties and establishes the method getDateSelected as the handler for the SelectionChanged event.

<asp:Calendar id=Calendar2
   onSelectionChanged = "getDateSelected"
   SelectionMode = "DayWeekMonth"
   Font-Name = "Verdana"
   Font-Size = "12px"
   NextPrevFormat = "ShortMonth"
   SelectWeekText = "week"
   SelectMonthText = "month"
   runat="server">

   <TodayDayStyle Font-Bold = "True" />

   <DayHeaderStyle Font-Bold = "True" />

   <OtherMonthDayStyle ForeColor = "gray" />

   <TitleStyle BackColor = "#3366ff"
      ForeColor = "white"
      Font-Bold = "True" />

   <SelectedDayStyle BackColor = "#ffcc66"
      Font-Bold = "True" />

   <NextPrevStyle ForeColor = "white"
      Font-Size = "10px" />

   <SelectorStyle BackColor = "#99ccff"
      ForeColor = "navy"
      Font-Size = "9px" />

</asp:Calendar>

The following example shows an event-handling method for a Calendar control’s SelectionChanged event. By querying the SelectedDates parameter of the Calendar control, you can determine how many days are selected, and therefore whether the user selects a day, week, or month. Information about the selection is displayed in a Label Web server control.

void getDateSelected ( object s, EventArgs e ) {
   switch ( myCalendar.SelectedDates.Count ) {
      case 0:   //None
         Message.Text = "No dates are currently selected";
         break;
      case 1:   //Day
         Message.Text = "The selected date is " + 
            myCalendar.SelectedDate.ToShortDateString ( );
         break;
      case 7:   //Week
         Message.Text = "The selection is a week beginning " + 
            myCalendar.SelectedDate.ToShortDateString ( );
         break;
      default:    //Month
         Message.Text = "The selection is a month beginning " +  
            myCalendar.SelectedDate.ToShortDateString ( );
         break;
   }
}
  C# VB

See Also

Calendar Class   Calendar Web Server Control



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note