SelectedDatesCollection 类

定义

封装 DateTime 对象的集合,这些对象表示 Calendar 控件中的选定日期。Encapsulates a collection of DateTime objects that represent the selected dates in a Calendar control. 此类不能被继承。This class cannot be inherited.

public ref class SelectedDatesCollection sealed : System::Collections::ICollection
public sealed class SelectedDatesCollection : System.Collections.ICollection
type SelectedDatesCollection = class
    interface ICollection
    interface IEnumerable
Public NotInheritable Class SelectedDatesCollection
Implements ICollection
继承
SelectedDatesCollection
实现

示例

下面的代码示例演示如何以编程方式使用SelectedDatesCollectionCalendar在控件中选择日期。The following code example demonstrates how to programmatically use the SelectedDatesCollection class to select dates in the Calendar control.

<%@ Page Language="C#"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  void Page_Load(Object sender, EventArgs e) 
  {
    DisplayCalendar.VisibleDate = DisplayCalendar.TodaysDate;
  }

  void SelectButton_Click(Object sender, EventArgs e) 
  {

    int current_day = DisplayCalendar.VisibleDate.Day;
    int current_month = DisplayCalendar.VisibleDate.Month;
    int current_year = DisplayCalendar.VisibleDate.Year;

    DisplayCalendar.SelectedDates.Clear();
   
    // Iterate through the current month and add all Wednesdays to the 
    // SelectedDates collection of the Calendar control.
    for (int i = 1; i <= System.DateTime.DaysInMonth(current_year, current_month); i++)
    {
       DateTime currentDate = new DateTime(current_year, current_month, i);
       if (currentDate.DayOfWeek == DayOfWeek.Wednesday)
       {
         DisplayCalendar.SelectedDates.Add(currentDate);
       }
    }

     MessageLabel.Text = "Selection Count = " + DisplayCalendar.SelectedDates.Count.ToString();
 
  }

  void DisplayCalendar_SelectionChanged(Object sender, EventArgs e) 
  {
    MessageLabel.Text = "Selection Count = " + DisplayCalendar.SelectedDates.Count.ToString();
  }
 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
 
      <asp:calendar id="DisplayCalendar" runat="server"  
        selectionmode="DayWeekMonth" 
        onselectionchanged="DisplayCalendar_SelectionChanged" />
 
      <hr />
 
      <asp:button id="SelectButton"
        text="Select All Weds in Month" 
        onclick="SelectButton_Click"  
        runat="server"/> 
        
      <br/>
 
      <asp:label id="MessageLabel" 
        runat="server" />
 
    </form>
  </body>
</html>
   
<%@ Page Language="VB"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
 
    DisplayCalendar.VisibleDate = DisplayCalendar.TodaysDate
    
  End Sub

  Sub SelectButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    Dim current_day As Integer = DisplayCalendar.VisibleDate.Day
    Dim current_month As Integer = DisplayCalendar.VisibleDate.Month
    Dim current_year As Integer = DisplayCalendar.VisibleDate.Year

    DisplayCalendar.SelectedDates.Clear()
   
    ' Iterate through the current month and add all Wednesdays to the 
    ' SelectedDates collection of the Calendar control.
    Dim i As Integer
    For i = 1 To System.DateTime.DaysInMonth(current_year, current_month)
    
      Dim currentDate As New DateTime(current_year, current_month, i)
      If currentDate.DayOfWeek = DayOfWeek.Wednesday Then
       
        DisplayCalendar.SelectedDates.Add(currentDate)
        
      End If
      
    Next

    MessageLabel.Text = "Selection Count = " + DisplayCalendar.SelectedDates.Count.ToString()
 
  End Sub

  Sub DisplayCalendar_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
  
    MessageLabel.Text = "Selection Count = " & DisplayCalendar.SelectedDates.Count.ToString()
  
  End Sub
 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
 
      <asp:calendar id="DisplayCalendar" runat="server"  
        selectionmode="DayWeekMonth" 
        onselectionchanged="DisplayCalendar_SelectionChanged" />
 
      <hr />
 
      <asp:button id="SelectButton"
        text="Select All Weds in Month" 
        onclick="SelectButton_Click"  
        runat="server"/> 
        
      <br/>
 
      <asp:label id="MessageLabel" 
        runat="server" />
 
    </form>
  </body>
</html>
   

注解

使用此类以编程方式管理System.DateTime对象的集合, 这些对象表示Calendar控件中的选定日期。Use this class to programmatically manage a collection of System.DateTime objects that represent the selected dates in a Calendar control. 此类通常用于在集合中添加或删除日期。This class is commonly used to add or remove dates from the collection.

此集合仅存储整个日期。This collection stores only whole dates. 删除每个System.DateTime的时间部分。The time portion of each System.DateTime is removed. 日期按升序存储。The dates are stored in ascending order. 如果有重复日期, 则集合中只存储一个日期。If there are duplicate dates, only one date is stored in the collection.

构造函数

SelectedDatesCollection(ArrayList)

用指定的日期列表初始化 SelectedDatesCollection 类的新实例。Initializes a new instance of the SelectedDatesCollection class with the specified date list.

属性

Count

获取 SelectedDatesCollection 集合中的 DateTime 对象数。Gets the number of DateTime objects in the SelectedDatesCollection collection.

IsReadOnly

获取指示 SelectedDatesCollection 集合是否为只读的值。Gets a value indicating whether the SelectedDatesCollection collection is read-only.

IsSynchronized

获取一个值,该值指示对 SelectedDatesCollection 集合的访问是否同步(线程安全)。Gets a value indicating whether access to the SelectedDatesCollection collection is synchronized (thread safe).

Item[Int32]

获取 DateTime 集合中位于指定索引位置的 SelectedDatesCollection 对象。Gets a DateTime object at the specified index in the SelectedDatesCollection collection.

SyncRoot

获取可用于同步 SelectedDatesCollection 集合访问的对象。Gets the object that can be used to synchronize access to the SelectedDatesCollection collection.

方法

Add(DateTime)

将指定的 DateTime 对象追加到 SelectedDatesCollection 集合的结尾。Appends the specified DateTime object to the end of the SelectedDatesCollection collection.

Clear()

从集合中删除所有 DateTime 对象。Removes all DateTime objects from the collection.

Contains(DateTime)

返回一个值,该值指示 SelectedDatesCollection 集合是否包含指定的 DateTime 对象。Returns a value indicating whether the SelectedDatesCollection collection contains the specified DateTime object.

CopyTo(Array, Int32)

从指定的索引开始,将 SelectedDatesCollection 集合中的项复制到指定的 Array 中。Copies the items from the SelectedDatesCollection collection to the specified Array, starting with the specified index.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回一个实现了 IEnumerator 的对象,其中包含 DateTime 集合内的所有 SelectedDatesCollection 对象。Returns an IEnumerator-implemented object that contains all DateTime objects within the SelectedDatesCollection collection.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Remove(DateTime)

DateTime 集合中移除指定的 SelectedDatesCollection 对象。Removes the specified DateTime object from the SelectedDatesCollection collection.

SelectRange(DateTime, DateTime)

将指定的日期范围添加到 SelectedDatesCollection 集合中。Adds the specified range of dates to the SelectedDatesCollection collection.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅