SelectedDatesCollection Class
Encapsulates a collection of System.DateTime objects that represent the selected dates in a Calendar control. This class cannot be inherited.
For a list of all members of this type, see SelectedDatesCollection Members.
System.Object
System.Web.UI.WebControls.SelectedDatesCollection
[Visual Basic] 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
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
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 only stores whole dates. 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.
Example
[Visual Basic, C#] The following example demonstrates how to programmatically use the SelectedDatesCollection class to select dates in the Calendar control.
[Visual Basic] <%@ 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] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI.WebControls
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
SelectedDatesCollection Members | System.Web.UI.WebControls Namespace | Calendar | SelectedDates | System.DateTime