SelectedDates Property

Calendar.SelectedDates Property

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

[Visual Basic]
Public ReadOnly Property SelectedDates As SelectedDatesCollection
[C#]
public SelectedDatesCollection SelectedDates {get;}
[C++]
public: __property SelectedDatesCollection* get_SelectedDates();
[JScript]
public function get SelectedDates() : SelectedDatesCollection;

Property Value

A SelectedDatesCollection that contains a collection of System.DateTime objects representing the selected dates on the Calendar control. The default value is an empty SelectedDatesCollection.

Remarks

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.

Note   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.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to use the SelectedDates collection to determine the selected dates on the Calendar control.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script language="VB" runat="server">
        Sub Selection_Change(sender As Object, e As EventArgs)
            Label1.Text = "The selected date(s):" & ChrW(60) & "br" & ChrW(62)
            Dim i As Integer
            For i = 0 To Calendar1.SelectedDates.Count - 1
                Label1.Text &= Calendar1.SelectedDates(i).ToShortDateString() & ChrW(60) & "br" & ChrW(62)
            Next i
        End Sub 'Selection_Change 
   </script>
</head>     
<body>

   <form 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>


[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

   <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 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>


[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>
<head>

   <script language="JScript" runat="server">

      function Selection_Change(sender : Object, e : EventArgs) 
      {
         Label1.Text = "The selected date(s):" + "<br>";
         for (var i : int = 0; i <= Calendar1.SelectedDates.Count - 1; i++)
         {
            Label1.Text += Calendar1.SelectedDates[i].ToShortDateString() + "<br>";
         } 
      }

   </script>

</head>     
<body>

   <form 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>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Calendar Class | Calendar Members | System.Web.UI.WebControls Namespace | System.DateTime | SelectedDate | SelectionMode | CalendarSelectionMode | SelectionChanged | OnSelectionChanged

Show:
© 2016 Microsoft