Calendar.SelectedDates 属性

定义

获取 DateTime 对象的集合,这些对象表示 Calendar 控件上的选定日期。Gets a collection of DateTime objects that represent the selected dates on the Calendar control.

public:
 property System::Web::UI::WebControls::SelectedDatesCollection ^ SelectedDates { System::Web::UI::WebControls::SelectedDatesCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.SelectedDatesCollection SelectedDates { get; }
member this.SelectedDates : System.Web.UI.WebControls.SelectedDatesCollection
Public ReadOnly Property SelectedDates As SelectedDatesCollection

属性值

一个 SelectedDatesCollection,包含表示 DateTime 上的选定日期的 Calendar 对象的集合。A SelectedDatesCollection that contains a collection of DateTime objects representing the selected dates on the Calendar. 默认值为空的 SelectedDatesCollectionThe default value is an empty SelectedDatesCollection.

属性

示例

下面的代码示例演示如何使用SelectedDates集合确定Calendar控件上的选定日期。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>

<%@ Page Language="VB" 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="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 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>

注解

使用集合确定Calendar控件上当前选定的日期。 SelectedDatesUse the SelectedDates collection to determine the currently selected dates on the Calendar control.

SelectedDate 属性SelectedDates和集合密切相关。The SelectedDate property and the SelectedDates collection are closely related. CalendarSelectionMode.Day SelectedDate SelectedDates[0]当属性设置为时,为仅允许单个日期选择的模式,具有相同的值,并且SelectedDates.Count等于1。 SelectionModeWhen 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. CalendarSelectionMode.DayWeek CalendarSelectionMode.DayWeekMonth当属性设置为或时SelectedDates[0] SelectedDate ,允许多个日期选择的模式和具有相同的值。 SelectionModeWhen 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.

属性存储对象的System.DateTime集合。 SelectedDatesThe SelectedDates property stores a collection of System.DateTime objects.

当用户在Calendar控件上选择一周或每月时, SelectionChanged将引发事件。When the user selects a week or month on the Calendar control, the SelectionChanged event is raised. 选定的日期将添加到SelectedDates集合中,并替换以前的内容。The selected dates are added to the SelectedDates collection, replacing the previous contents. 日期范围按日期进行升序排序。The range of dates are sorted in ascending order by date. 还会更新SelectedDates属性以包含集合中的第一个日期。 SelectedDateThe SelectedDate property is also updated to contain the first date in the SelectedDates collection.

你还可以使用该SelectedDates集合以编程方式选择Calendar控件上的日期。You can also use the SelectedDates collection to programmatically select dates on the Calendar control. Add使用、 Remove、和方法SelectRange以编程方式操作SelectedDates集合中的选定日期。 ClearUse the Add, Remove, Clear, and SelectRange methods to programmatically manipulate the selected dates in the SelectedDates collection.

备注

在引发SelectionChanged事件之前, SelectedDates将更新属性和集合。SelectedDateBoth the SelectedDate property and the SelectedDates collection are updated before the SelectionChanged event is raised. 您可以通过使用OnSelectionChanged事件处理程序手动SelectedDates设置集合来覆盖日期选择。You can override the date selection by using the OnSelectionChanged event handler to manually set the SelectedDates collection. 如果以编程方式设置此集合,则不会引发事件。SelectionChangedThe SelectionChanged event is not raised when this collection is programmatically set.

适用于

另请参阅