asp.net - Calendar SelectedDatesCollection Class

Calendar SelectedDatesCollection Class
Calendar is an asp.net web server control which allow users to select an individual date or date range at a time. .net SelectedDatesCollection class encapsulates a collection of DateTime objects that represent the calendar selected date.

SelectedDatesCollection class has the following properties Count, IsReadOnly, IsSynchronized, Item and SyncRoot. Count property allow us to get the number of DateTime objects in the calendar selected dates collection. Item property get a DateTime object at the specified index in the selected dates collection.

SelectedDatesCollection class have many methods such as Add(), Clear(), Contains(), CopyTo(), Remove(), SelectRange() etc. Add() method append a date time object to the end of selected dates collection. Clear() method remove all the date time objects from calendar selected dates collection. Contains() method return a value indicating whether the calendar selected dates collection contains a specified date time object. Remove() method remove a specified item (date time object) from calendar selected dates collection. SelectRange() method adds the specified range of dates to the selected dates collection.

the following asp.net c# example code demonstrate us how can we use calendar SelectedDatesCollection class in an asp.net application.
SelectedDatesCollectionClass.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {
        SelectedDatesCollection dates = Calendar1.SelectedDates;
        Label1.Text = "You selected:...."+ dates.Count +" date(s)<br /><br />";
        
        for (int i = 0; i < dates.Count;i++ )
        {
            Label1.Text += dates[i].ToLongDateString() + "<br />";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Calendar control - How to use SelectedDatesCollection Class</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DodgerBlue; font-style:italic;">
            Calendar control - How to use SelectedDatesCollection Class
        </h2>
        <hr width="550" align="left" color="LightBlue" />
        <asp:Label
            ID="Label1" 
            runat="server"
            Font-Size="Large"
            ForeColor="DodgerBlue"
            Font-Italic="true"
            >
        </asp:Label>
        <br />
        <asp:Calendar
            ID="Calendar1" 
            runat="server"
            NextPrevFormat="FullMonth"
            SelectionMode="DayWeekMonth"
            SelectMonthText="Month"
            SelectWeekText="Week"
            ForeColor="WhiteSmoke"
            DayNameFormat="Full"
            Font-Names="Book Antiqua"
            Font-Size="Medium"
            OnSelectionChanged="Calendar1_SelectionChanged"
            >
            <DayHeaderStyle
                 BackColor="IndianRed"
                 />
            <DayStyle
                 BackColor="MediumPurple"
                 BorderColor="Purple"
                 BorderWidth="1"
                 Font-Bold="true"
                 Font-Italic="true"
                 />
            <NextPrevStyle
                 Font-Italic="true"
                 Font-Names="Arial CE"
                 />
            <SelectedDayStyle
                 BackColor="ForestGreen"
                 BorderColor="LightGreen"
                 />
            <SelectorStyle
                 BackColor="IndianRed"


                 ForeColor="Snow"
                 Font-Names="Times New Roman Greek"
                 Font-Size="Small"
                 BorderColor="DarkRed"
                 BorderWidth="1"
                 />
            <TitleStyle
                 BackColor="Firebrick"
                 Height="35"
                 Font-Size="Large"
                 Font-Names="Courier New Baltic"
                 />
        </asp:Calendar>
    </div>
    </form>
</body>
</html>
More asp.net examples