Partager via


How to: Control Month Navigation in a Calendar Web Server 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 in the title bar 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.

To enable or disable user month navigation

  • Set the Calendar control's ShowNextPrevMonth property to true or false. If the property is 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.

To navigate months programmatically

  • 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 (if 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:

    Calendar1.VisibleDate = New Date(2000, 2, 1)
    
    Calendar1.VisibleDate = new DateTime(2000, 2, 1);
    

    Changing the VisibleDate property has no effect on the values of the TodaysDate, SelectedDayStyle, 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 later than the end-date month.

To respond to a month navigation event

  • Create a method for the control's VisibleMonthChanged event. When this event is raised, the control has already changed the VisibleMonth property by 1.

    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:

    Property

    Description

    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 the direction in which 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.

    Public Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, _
       ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) _
       Handles Calendar1.VisibleMonthChanged
    
       ' Select the 10th and 25th of each month.
       Calendar1.SelectedDates.Clear()
       Calendar1.SelectedDates.Add(New DateTime(e.NewDate.Year, e.NewDate.Month, 10))
       Calendar1.SelectedDates.Add(New DateTime(e.NewDate.Year, e.NewDate.Month, 25))
    End Sub
    
    private void Calendar1_VisibleMonthChanged (object sender, 
       System.Web.UI.WebControls.MonthChangedEventArgs e)
    {
         // Select the 10th and 25th of each month.
         Calendar1.SelectedDates.Clear();
         Calendar1.SelectedDates.Add(new DateTime(e.NewDate.Year, e.NewDate.Month, 10));
         Calendar1.SelectedDates.Add(new DateTime(e.NewDate.Year, e.NewDate.Month, 25));
    }
    

See Also

Concepts

Calendar Web Server Control Overview