ASP.NET – Calendar Control


Adding a calendar in ASP.NET and displaying it in a webpage is not difficult. One of my friend asked me about the same because he never used this control. So he asked me about sample or demo of Calender Control in ASP.NET.

The Calendar control is a functionally rich web control, which provides the following capabilities

  • Displaying one month at a time
  • Selecting a day, a week or a month
  • Selecting a range of days
  • Moving from month to month
  • Controlling the display of the days programmatically

The basic syntax of a calendar control is :

<asp:Calender ID = "Calendar1" runat = "server">
</asp:Calender>

Properties and Events of the Calendar Control

The calendar control has many properties and events, using which you can customize the actions and display of the control. The following table provides some important properties of the Calendar control :

Properties Description
Caption Gets or sets the caption for the calendar control.
CaptionAlign Gets or sets the alignment for the caption.
CellPadding Gets or sets the number of spaces between the data and the cell border.
CellSpacing Gets or sets the space between cells.
DayHeaderStyle Gets the style properties for the section that displays the day of the week.
DayNameFormat Gets or sets format of days of the week.
DayStyle Gets the style properties for the days in the displayed month.
FirstDayOfWeek Gets or sets the day of week to display in the first column.
NextMonthText Gets or sets the text for next month navigation control. The default value is >.
NextPrevFormat Gets or sets the format of the next and previous month navigation control.
OtherMonthDayStyle Gets the style properties for the days on the Calendar control that are not in the displayed month.
PrevMonthText Gets or sets the text for previous month navigation control. The default value is <.
SelectedDate Gets or sets the selected date.
SelectedDates Gets a collection of DateTime objects representing the selected dates.
SelectedDayStyle Gets the style properties for the selected dates.
SelectionMode Gets or sets the selection mode that specifies whether the user can select a single day, a week or an entire month.
SelectMonthText Gets or sets the text for the month selection element in the selector column.
SelectorStyle Gets the style properties for the week and month selector column.
SelectWeekText Gets or sets the text displayed for the week selection element in the selector column.
ShowDayHeader Gets or sets the value indicating whether the heading for the days of the week is displayed.
ShowGridLines Gets or sets the value indicating whether the gridlines would be shown.
ShowNextPrevMonth Gets or sets a value indicating whether next and previous month navigation elements are shown in the title section.
ShowTitle Gets or sets a value indicating whether the title section is displayed.
TitleFormat Gets or sets the format for the title section.
Titlestyle Get the style properties of the title heading for the Calendar control.
TodayDayStyle Gets the style properties for today’s date on the Calendar control.
TodaysDate Gets or sets the value for today’s date.
UseAccessibleHeader Gets or sets a value that indicates whether to render the table header <th> HTML element for the day headers instead of the table data <td> HTML element.
VisibleDate Gets or sets the date that specifies the month to display.
WeekendDayStyle Gets the style properties for the weekend dates on the Calendar control.

The Calendar control has the following three most important events that allow the developers to program the calendar control. They are :

Events Description
SelectionChanged It is raised when a day, a week or an entire month is selected.
DayRender It is raised when each data cell of the calendar control is rendered.
VisibleMonthChanged It is raised when user changes a month.

Example
calender
The following example demonstrates selecting a date and displays the date in a label. The content file code is as follows

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CalenderDemo._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <center>
        <div style="padding:1em;font-size:large"><b>Calender Control Demo</b></div>
        <div>
            <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px">
                <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
                <NextPrevStyle VerticalAlign="Bottom" />
                <OtherMonthDayStyle ForeColor="#808080" />
                <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
                <SelectorStyle BackColor="#CCCCCC" />
                <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
                <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
                <WeekendDayStyle BackColor="#FFFFCC" />
            </asp:Calendar>
            <asp:Label ID="Label1" runat="server" Text="Selected Date : " ></asp:Label>
            <asp:Label ID="lblDate" runat="server" Text="" Font-Bold="true"></asp:Label>
        </div>
        <div style="vertical-align:bottom;padding-top:10em">Read My Articles and Latest News Here : <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="https://manojbhoir.wordpress.com/">Manoj Bhor - My Passion Towards Technology</asp:HyperLink></div>
    </center>
    </form>
</body>
</html>

The event handler for the event SelectionChanged :

 protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            lblDate.Text = this.Calendar1.SelectedDate.ToString();
        }

Download

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Google+ photo

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

Connecting to %s