Prop Webcontrol Calendar Selecteddate
## ASP.NET Calendar SelectedDate Property
The `SelectedDate` property is a core feature of the ASP.NET Web Forms `Calendar` control. It is used to get or set the currently selected date on the calendar.
---
## Definition and Usage
The `SelectedDate` property allows developers to programmatically determine which date a user has clicked, or to pre-select a specific date when the page loads.
* **Default Value:** `DateTime.MinValue` (which represents `01/01/0001 00:00:00`).
* **Behavior:** If multiple dates are selected (when `SelectionMode` is set to `DayWeek` or `DayWeekMonth`), the `SelectedDate` property returns the first date in the selected range.
---
## Syntax
### ASP.NET Markup (Declarative Syntax)
```asp
```
### C# (Code-Behind)
```csharp
// Get the selected date
DateTime selected = Calendar1.SelectedDate;
// Set the selected date
Calendar1.SelectedDate = new DateTime(2023, 10, 25);
```
### VB.NET (Code-Behind)
```vb
' Get the selected date
Dim selected As DateTime = Calendar1.SelectedDate
' Set the selected date
Calendar1.SelectedDate = New DateTime(2023, 10, 25)
```
---
## Code Examples
### Example 1: Displaying the Selected Date (VB.NET)
The following example demonstrates how to capture the `SelectionChanged` event and output the selected date to the user using VB.NET.
```asp
```
### Example 2: Displaying the Selected Date (C#)
This is the equivalent implementation using C# and a Label control to display the selected date cleanly.
```asp
<%@ Page Language="C#" %>
Calendar SelectedDate Example
```
---
## Key Considerations
1. **Date Range Selection:** If you allow users to select multiple dates (e.g., a whole week or month), use the `SelectedDates` collection property instead of `SelectedDate`. `SelectedDate` will only return the first date of that collection.
2. **Clearing Selection:** To clear the selected date programmatically, you can clear the `SelectedDates` collection:
```csharp
Calendar1.SelectedDates.Clear();
```
3. **Initial Value:** If no date is selected by the user and you have not set one programmatically, `SelectedDate` returns `01/01/0001`. It is good practice to check if a date has been selected before processing it:
```csharp
if (Calendar1.SelectedDate != DateTime.MinValue)
{
// Process the date
}
```
YouTip