SelectedDatesCollection クラス

Calendar コントロールで選択されている日付を表す System.DateTime オブジェクトのコレクションをカプセル化します。このクラスは継承できません。

この型のすべてのメンバの一覧については、SelectedDatesCollection メンバ を参照してください。

System.Object
   System.Web.UI.WebControls.SelectedDatesCollection

NotInheritable Public Class SelectedDatesCollection
   Implements ICollection, IEnumerable
[C#]
public sealed class SelectedDatesCollection : ICollection,
   IEnumerable
[C++]
public __gc __sealed class SelectedDatesCollection : public
   ICollection, IEnumerable
[JScript]
public class SelectedDatesCollection implements ICollection,
   IEnumerable

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

このクラスを使用して、 Calendar コントロールで選択されている日付を表す System.DateTime オブジェクトのコレクションをプログラムによって管理します。通常、このクラスはコレクションから日付を追加または削除するために使用します。

このコレクションは、日付全体を格納するだけです。各 System.DateTime の時間部分は削除されます。日付は昇順で格納されます。重複する日付がある場合、コレクションに格納されるのは 1 日だけです。

使用例

[Visual Basic, C#] プログラムによって SelectedDatesCollection クラスを使用し、 Calendar コントロールで日付を選択する方法の例を次に示します。

 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
 <head>
 
    <script runat="server">

        Sub Page_Load(sender As Object, e As EventArgs)
            Calendar1.VisibleDate = Calendar1.TodaysDate
        End Sub

        Sub Button_Click(sender As Object, e As EventArgs)
            
            Dim current_day As Integer = Calendar1.VisibleDate.Day
            Dim current_month As Integer = Calendar1.VisibleDate.Month
            Dim current_year As Integer = Calendar1.VisibleDate.Year
            
            Calendar1.SelectedDates.Clear()
            
            ' Iterate through the current month and add all Wednesdays to the collection.
            Dim i As Integer
            For i = 1 To System.DateTime.DaysInMonth(current_year, current_month)
                Dim theDate As New DateTime(current_year, current_month, i)
                If theDate.DayOfWeek = DayOfWeek.Wednesday Then
                    Calendar1.SelectedDates.Add(theDate)
                End If
            Next i 
            Label1.Text = "Selection Count = " & _
                Calendar1.SelectedDates.Count.ToString()
        End Sub
         
        Sub Selection_Change(sender As Object, e As EventArgs)
            Label1.Text = "Selection Count = " & _
                Calendar1.SelectedDates.Count.ToString()
        End Sub
 
    </script>
 
 </head>     
 <body>
 
    <form runat="server">
 
       <asp:Calendar ID="Calendar1" runat="server"  
            SelectionMode="DayWeekMonth" 
            OnSelectionChanged="Selection_Change" />
 
       <hr>
 
       <asp:Button id="Button1"
            text="Select All Weds in Month" 
            OnClick="Button_Click"  
            runat=server  /> <br>
 
       <asp:Label id="Label1" runat=server />
 
    </form>
 </body>
 </html>
   

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

<html>
 <head>
 
    <script runat="server">
 
       void Page_Load(Object sender, EventArgs e) 
       {
          Calendar1.VisibleDate = Calendar1.TodaysDate;
       }
 
       void Button_Click(Object sender, EventArgs e) 
       {
   
          int current_day = Calendar1.VisibleDate.Day;
          int current_month = Calendar1.VisibleDate.Month;
          int current_year = Calendar1.VisibleDate.Year;
   
          Calendar1.SelectedDates.Clear();
   
          // Iterate through the current month and add all Wednesdays to the collection.
          for (int i = 1; i <= System.DateTime.DaysInMonth(current_year, current_month); i++)
          {
             DateTime date = new DateTime(current_year, current_month, i);
             if (date.DayOfWeek == DayOfWeek.Wednesday)
                Calendar1.SelectedDates.Add(date);
          }
 
          Label1.Text = "Selection Count = " + Calendar1.SelectedDates.Count.ToString();
 
       }
 
       void Selection_Change(Object sender, EventArgs e) 
       {
          Label1.Text = "Selection Count = " + Calendar1.SelectedDates.Count.ToString();
       }
 
    </script>
 
 </head>     
 <body>
 
    <form runat="server">
 
       <asp:Calendar ID="Calendar1" runat="server"  
            SelectionMode="DayWeekMonth" 
            OnSelectionChanged="Selection_Change" />
 
       <hr>
 
       <asp:Button id="Button1"
            text="Select All Weds in Month" 
            OnClick="Button_Click"  
            runat=server  /> <br>
 
       <asp:Label id="Label1" runat=server />
 
    </form>
 </body>
 </html>
   

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Web.UI.WebControls

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Web (System.Web.dll 内)

参照

SelectedDatesCollection メンバ | System.Web.UI.WebControls 名前空間 | Calendar | SelectedDates | System.DateTime