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:
NoteThe 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 |
|---|---|
|
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. |
|
|
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.
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
-
Get the count of selected dates using the Count property of the SelectedDates property.
-
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.