SelectedDatesCollection Class
Assembly: System.Web (in system.web.dll)
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. 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.
The following code example demonstrates how to programmatically use the SelectedDatesCollection class to select dates in the Calendar control.
<%@ 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>
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.