ASP.NET Calendar Class

The Calendar class displays a single month calendar that allows the user to select dates and move to the next or previous month. It is part of the System.Web.UI.WebControls namespace.

There are several style properties (color, border, cell, etc) that can be set to change the look and feel of the control. Below are the most useful (non-style) properties.

DayNameFormat
Gets or sets the name format of days of the week.

FirstDayOfWeek
Gets or sets the day of the week to display in the first day column of the Calendar control.

SelectedDate
Gets or sets the selected date.

SelectedDates
Gets a collection of System.DateTime objects that represent the selected dates on the Calendar control.

SelectionMode
Gets or sets the date selection mode on the Calendar control that specifies whether the user can select a single day, a week, or an entire month.

TodaysDate
Gets or sets the value for today’s date.

VisibleDate
Gets or sets the date that specifies the month to display on the Calendar control.

Simple Example of the Calendar Control

ASP.NET SOURCE

<asp:Calendar
   ID="Calendar1" runat="server"
   BackColor="#FFFFCC" BorderColor="#FFCC66"
   BorderWidth="1px"
   DayNameFormat="Shortest"
   Font-Names="Verdana" Font-Size="8pt"
   ForeColor="#663399" Height="200px"
   ShowGridLines="True" Width="220px"
   OnSelectionChanged="Calendar1_SelectionChanged">
   <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
   <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
   <SelectorStyle BackColor="#FFCC66" />
   <OtherMonthDayStyle ForeColor="#CC9966" />
   <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
   <DayHeaderStyle BackColor="#FFCC66"
      Font-Bold="True" Height="1px" />
   <TitleStyle BackColor="#990000"
      Font-Bold="True" Font-Size="9pt"
      ForeColor="#FFFFCC" />
</asp:Calendar>

C# SOURCE

using System.Web;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
      Label1.Text = Calendar1.TodaysDate.ToString();
  }
  protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  {
      Label1.Text = Calendar1.SelectedDate.ToString();
  }
}

RESULT

The Calendar control is a rich control that can be extended much further. Online appointment calendars and event calendars are just the beginning.

Posted on March 18, 2007, in ASP.NET. Bookmark the permalink. Leave a Comment.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.