Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Calendar.SelectedDates Property

Gets a collection of System.DateTime objects that represent the selected dates on the Calendar control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
[BrowsableAttribute(false)]
public SelectedDatesCollection SelectedDates { get; }

Property Value

Type: System.Web.UI.WebControls.SelectedDatesCollection
A SelectedDatesCollection that contains a collection of System.DateTime objects representing the selected dates on the Calendar. The default value is an empty SelectedDatesCollection.

Use the SelectedDates collection to determine the currently selected dates 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 SelectedDates property stores a collection of System.DateTime objects.

When the user selects a week or month on the Calendar control, the SelectionChanged event is raised. The selected dates are added to the SelectedDates collection, replacing the previous contents. The range of dates are sorted in ascending order by date. The SelectedDate property is also updated to contain the first date in the SelectedDates collection.

You can also use the SelectedDates collection to programmatically select dates on the Calendar control. Use the Add, Remove, Clear, and SelectRange methods to programmatically manipulate the selected dates in the SelectedDates collection.

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 SelectedDates collection. The SelectionChanged event is not raised when this collection is programmatically set.

The following code example demonstrates how to use the SelectedDates collection to determine the selected dates 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(s):" + "<br />";
         for (int i = 0; i <= Calendar1.SelectedDates.Count - 1; i++)
         {
            Label1.Text += Calendar1.SelectedDates[i].ToShortDateString() + "<br />";
         } 
      }

   </script>

</head>     
<body>

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

      <h3>Calendar Example</h3>

      Select date(s) on the Calendar control.<br /><br />

      <asp:Calendar ID="Calendar1" runat="server"  
           SelectionMode="DayWeekMonth" 
           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

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.