SelectedDatesCollection 类

封装 System.DateTime 对象的集合,这些对象表示 Calendar 控件中的选定日期。无法继承此类。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
Public NotInheritable Class SelectedDatesCollection
    Implements ICollection, IEnumerable
用法
Dim instance As SelectedDatesCollection
public sealed class SelectedDatesCollection : ICollection, IEnumerable
public ref class SelectedDatesCollection sealed : ICollection, IEnumerable
public final class SelectedDatesCollection implements ICollection, IEnumerable
public final class SelectedDatesCollection implements ICollection, IEnumerable

备注

使用此类以编程方式管理 System.DateTime 对象的集合,这些对象表示 Calendar 控件中的选定日期。该类通常用于向集合中添加日期,或者是移除集合中的日期。

此集合仅存储完整的日期。每个 System.DateTime 的时间部分将被移除。日期以升序方式存储。如果有重复的日期,则只有一个日期存储在集合中。

示例

下面的代码示例演示如何以编程方式使用 SelectedDatesCollection 类在 Calendar 控件中选择日期。

<%@ Page Language="VB"%>
 
<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>
  <body>
    <form 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="C#"%>
 
<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>
  <body>
    <form 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.Object
  System.Web.UI.WebControls.SelectedDatesCollection

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

SelectedDatesCollection 成员
System.Web.UI.WebControls 命名空间
Calendar 类
Calendar.SelectedDates 属性
System.DateTime