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
<asp:Calendar
AccessKey = "string"
BackColor = "color name | #dddddd"
BorderColor = "color name | #dddddd"
BorderStyle = "NotSet | None | Dotted | Dashed | Solid | Double | Groove |
Ridge | Inset | Outset"
BorderWidth = size
Caption = "string"
CaptionAlign = "NotSet | Top | Bottom | Left | Right"
CellPadding = integer
CellSpacing = integer
CssClass = "string"
DayNameFormat = "Full | Short | FirstLetter | FirstTwoLetters | Shortest"
Enabled = "True | False"
EnableTheming = "True | False"
EnableViewState = "True | False"
FirstDayOfWeek = "Sunday | Monday | Tuesday | Wednesday | Thursday |
Friday | Saturday | Default"
Font-Bold = "True | False"
Font-Italic = "True | False"
Font-Names = "string"
Font-Overline = "True | False"
Font-Size = "string | Smaller | Larger | XX-Small | X-Small | Small |
Medium | Large | X-Large | XX-Large"
Font-Strikeout = "True | False"
Font-Underline = "True | False"
ForeColor = "color name | #dddddd"
Height = size
ID = "string"
NextMonthText = "string"
NextPrevFormat = "CustomText | ShortMonth | FullMonth"
OnDataBinding = "DataBinding event handler"
OnDayRender = "DayRender event handler"
OnDisposed = "Disposed event handler"
OnInit = "Init event handler"
OnLoad = "Load event handler"
OnPreRender = "PreRender event handler"
OnSelectionChanged = "SelectionChanged event handler"
OnUnload = "Unload event handler"
OnVisibleMonthChanged = "VisibleMonthChanged event handler"
PrevMonthText = "string"
runat = "server"
SelectedDate = "string"
SelectionMode = "None | Day | DayWeek | DayWeekMonth"
SelectMonthText = "string"
SelectWeekText = "string"
ShowDayHeader = "True | False"
ShowGridLines = "True | False"
ShowNextPrevMonth = "True | False"
ShowTitle = "True | False"
SkinID = "string"
Style = "string"
TabIndex = integer
TitleFormat = "Month | MonthYear"
ToolTip = "string"
UseAccessibleHeader = "True | False"
Visible = "True | False"
VisibleDate = "string"
Width = size
>
<DayHeaderStyle />
<DayStyle />
<NextPrevStyle />
<OtherMonthDayStyle />
<SelectedDayStyle />
<SelectorStyle />
<TitleStyle />
<TodayDayStyle />
<WeekendDayStyle />
</asp:Calendar>
For information on the individual members of this class, see Calendar in the class library.
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.
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;
}
}
Sub getDateSelected ( sender as Object sender, e As EventArgs )
Select ( myCalendar.SelectedDates.Count )
Case 0: ’None
Message.Text = "No dates are currently selected"
Case 1: ’ day
Message.Text = "The selected date is " & _
myCalendar.SelectedDate.ToShortDateString
Case 7: ’Week
Message.Text = "The selection is a week beginning " & _
myCalendar.SelectedDate.ToShortDateString
Case Else: ’Month
Message.Text = "The selection is a month beginning " & _
myCalendar.SelectedDate.ToShortDateString
End Select
End Sub |
|
C# |
VB |
Calendar Class Calendar Web Server Control