How to: Respond to Date Selection in a Calendar Web Server Control

If the Calendar control's SelectionMode property is set to anything other than None, the user can select a day or a range of dates. You can detect and respond to the user's choice.

To respond to a date selection

  • Create a method for the Calendar control's SelectionChanged event with the following signature:

    Private Sub Calendar1_SelectionChanged(ByVal sender _
        As System.Object, ByVal e As System.EventArgs) _
        Handles Calendar1.SelectionChanged
    
    private void Calendar1_SelectionChanged (object sender, 
        System.EventArgs e)
    

    Note

    The event is raised only if the date selection is changed by user action in the control. For instance, if the user clicks the same date twice, the second click does not raise an event. The event is also not raised if you set a date range programmatically.

Information about date selection is available in these properties:

Property

Description

SelectedDate

A single date. If the user has selected a single date, this property contains that date. If the user has selected multiple dates, this property contains the first date in the range.

SelectedDates

A collection containing all the dates selected. The dates in this collection are ordered and unique. Because the Calendar control does not allow the user to select multiple individual dates, the dates in the collection are also sequential.

To determine how many dates are selected

  • Get the value of the Count property of the SelectedDates collection, as shown in the following example.

    Public Sub Calendar1_SelectionChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles Calendar1.SelectionChanged
    
       Text1.Text = "You selected " _
          & Calendar1.SelectedDates.Count.ToString() _
          & " date(s)."
    End Sub
    
    private void Calendar1_SelectionChanged (object sender, System.EventArgs e)
    {
       Text1.Text = String.Format("You selected {0} date(s).",
          Calendar1.SelectedDates.Count);
    }   
    

If you've determined that the user has selected multiple dates, you can get the range.

To get the date range of a multi-date selection

  1. Get the count of selected dates using the Count property of the SelectedDates property.

  2. Get the first date in the collection, and then get the last date by extracting the date at the index of the count minus one. The following example displays the first and last dates in text box controls on the page.

    Public Sub Calendar1_SelectionChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles Calendar1.SelectionChanged
    
       With Calendar1.SelectedDates
          If .Count > 1 Then
             Dim firstDate As Date = .Item(0).Date
             Dim lastDate As Date = .Item(.Count - 1).Date
             TextBox1.Text = firstDate.ToString()
             TextBox2.Text = lastDate.ToString()
          End If
       End With
    End Sub
    
    private void Calendar1_SelectionChanged (object sender,
       System.EventArgs e)
    {
       SelectedDatesCollection theDates = Calendar1.SelectedDates;
       if (theDates.Count > 1) 
       {
          DateTime firstDate = theDates[0];
          DateTime lastDate = theDates[theDates.Count-1];
          TextBox1.Text = firstDate.ToString();
          TextBox2.Text = lastDate.ToString();
       }
    }
    

To get the time span of a multi-date selection

  • Create a TimeSpan object and set it to the difference between the last and first dates in the SelectedDates collection, and then get the TimeSpan object's Days property.

    Public Sub Calendar1_SelectionChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
       With Calendar1.SelectedDates
          Dim days As Integer
          Dim firstDate As Date = .Item(0).Date
          Dim lastDate As Date = .Item(.Count - 1).Date
          ' The Subtract method returns a TimeSpan object.
          days = lastDate.Subtract(firstDate).Days + 1
          TextBox1.Text = "You have selected " & days.ToString() & " day(s)."
       End With
    End Sub
    
    private void Calendar1_SelectionChanged (object sender,
       System.EventArgs e)
    {
       SelectedDatesCollection theDates = Calendar1.SelectedDates;
       TimeSpan timeSpan = theDates[theDates.Count-1] - theDates[0];
       TextBox1.Text = 
          String.Format("You have selected {0} day(s).", timeSpan.Days + 1);
    }   
    

See Also

Tasks

How to: Select Dates Programmatically in a Calendar Web Server Control

Concepts

Calendar Web Server Control Overview