asp.net.ph

Skip Navigation Links

Responding to Date Selection

Controls You Can Use on Web Forms   ASP.NET Standard Controls   Calendar Control


If the Calendar control’s SelectionMode 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:
Public Sub control_SelectionChanged ( ByValControls As Object, _
   ByVal e As EventArgs ) 

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:

  • SelectedDate is 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 is 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 ( ByValControls As Object, _
      ByVal e As EventArgs )
   Text1.Text = "You selected " & _
      Calendar1.SelectedDates.Count " & date ( s ) ."
End Sub

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.
Public Sub Calendar1_SelectionChanged ( ByValControls As Object, _
      ByVal e As DayRenderEvent )
   If Calendar1.SelectedDates.Count > 1 Then
      Dim FirstDate as DateTime
      Dim LastDate as DateTime
      FirstDate = SelectedDates [ 0 ] 
      LastDate = SelectedDates [ SelectedDates.Count - 1 ] 
      txtFirstDate.Text = FirstDate.toString
      txtLastDate.Text = LastDate.toString
   End If
End Sub

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_SelectedionChanged ( ByValControls As Object, _
      ByVal e As DayRenderEvent )
   Dim ts as TimeSpan
   ts = Calendar1.SelectedDates [ SelectedDates.Count - 1 ] - _
   Calendar1.SelectedDates [ 0 ] 
   Text1.Text = "You selected " & ts.Days+1 & " days."
End Sub


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note