Export (0) Print
Expand All

Responding to Date Selection in a Calendar Web Server Control

Visual Studio .NET 2003

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:
    ' Visual Basic
    Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
    
    // C#
    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:

PropertyDescription
SelectedDateA 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.
SelectedDatesA 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.
    ' Visual Basic
    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
    
    // C#
    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 -1. The following example displays the first and last dates in TextBox controls on the page.
    ' Visual Basic
    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
    
    // C#
    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.
    ' Visual Basic
    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
    
    // C#
    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

Setting Today's Date Programmatically in a Calendar Web Server Control | Selecting Dates Programmatically in a Calendar Web Server Control

Show:
© 2014 Microsoft