Calendar.SelectedDate Property

 

Gets or sets the selected date.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[BindableAttribute(true, BindingDirection.TwoWay)]
public DateTime SelectedDate { get; set; }

Property Value

Type: System.DateTime

A System.DateTime that represents the selected date. The default value is DateTime.MinValue.

Use the SelectedDate property to determine the selected date on the Calendar control.

The SelectedDate property and the SelectedDates collection are closely related. When the SelectionMode property is set to CalendarSelectionMode.Day, a mode that allows only a single date selection, SelectedDate and SelectedDates[0] have the same value and SelectedDates.Count equals 1. When the SelectionMode property is set to CalendarSelectionMode.DayWeek or CalendarSelectionMode.DayWeekMonth, modes that allows multiple date selections, SelectedDate and SelectedDates[0] have the same value.

The SelectedDate property is set using a System.DateTime object.

When the user selects a date on the Calendar control, the SelectionChanged event is raised. The SelectedDate property is updated to the selected date. The SelectedDates collection is also updated to contain just this date.

System_CAPS_noteNote

Both the SelectedDate property and the SelectedDates collection are updated before the SelectionChanged event is raised. You can override the date selection by using the OnSelectionChanged event handler to manually set the SelectedDate property. The SelectionChanged event does not get raised when this property is programmatically set.

The following code example demonstrates how to use the SelectedDate property to determine the selected date on the Calendar control.

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Calendar Example</title>
<script language="C#" runat="server">

      void Selection_Change(Object sender, EventArgs e) 
      {
         Label1.Text = "The selected date is " + Calendar1.SelectedDate.ToShortDateString();
      }

   </script>

</head>     
<body>

   <form id="form1" runat="server">

      <h3>Calendar Example</h3>

      Select a date on the Calendar control.<br /><br />

      <asp:Calendar ID="Calendar1" runat="server"  
           SelectionMode="Day" 
           ShowGridLines="True"
           OnSelectionChanged="Selection_Change">

         <SelectedDayStyle BackColor="Yellow"
                           ForeColor="Red">
         </SelectedDayStyle>

      </asp:Calendar>     

      <hr /><br />

      <asp:Label id="Label1" runat="server" />

   </form>
</body>
</html>

.NET Framework
Available since 1.1
Return to top
Show: