Controls You Can Use on Web Forms ASP.NET Standard Controls Calendar Control
By default, the calendar displays the month containing today’s date. Users can move from month to month in the Calendar control by clicking month navigation links ( actually, LinkButton Web server controls ) in the titlebar of the calendar. You can control whether these links are available for the user to click.
You can also navigate programmatically, which is useful if you want to provide an alternate way for users to display a specific month. Finally, you can respond to an event raised when the user navigates.
- Set the Calendar control’s ShowNextPrevMonth property to true or false. If false, the control does not display the LinkButton controls in the title that allow a user to move between months.
TIP: You can change the appearance of the month navigation hyperlinks by setting the NextMonthText, PrevMonthText, NextPrevFormat, and NextPrevStyle properties.
- Set the control’s VisibleDate property to a date in the month you want to display. The date can be any day within that month, but is typically set to the first day of the month. If the VisibleDate property is empty ( it is set to DateTime.Empty ), the currently visible month is derived from the value of the TodaysDate property.
The following example navigates to February, 2000:
[ Visual Basic ]
[ C# ]
Calendar1.VisibleDate = new DateTime ( 2000, 2, 1 );
Changing the VisibleDate property has no effect on the values of the TodaysDate, SelectedDay, or SelectedDates properties.
If month navigation is enabled, the control raises an event when the user moves to another month. You can handle this event in order to replace or amend the default month navigation. For example, if you are using two Calendar controls in a trip planning page, you can prevent the user from setting the start-date month earlier than the end-date month.
- Create a method for the control’s VisibleMonthChanged event. When this event is raised, the control has already changed the VisibleMonth property by one.
The VisibleMonthChanged event takes a single argument of type MonthChangedEventArgs. You can use the following properties of this argument to determine or override what the user is doing:
- PreviousDate The value of the month that was displayed before the user clicked a month navigation button. You can compare the value of this property to that of the control’s VisibleMonth property to determine what direction the user is navigating. To cancel the effect of the user’s click, set the control’s VisibleDate property to this value.
- NewDate The value of the month that the user has navigated to. The VisibleDate property is updated to this value before the VisibleMonthChanged event is raised.