The Calendar control displays a calender that allows users to navigate to any day of the year. When the ASP.net Web page runs, the Calendar control renders as an HTML table. As a result, many of the properties of the control correspond to a number of different table formats. Several of these properties are not fully supported in some of the lower versions of browsers, so all formatting features are not available in these browsers.
Use the Calendar control to display a single month of a calendar on a Web page. The control allows you to select a date and move to the next or last month. The Calendar control supports all System.Globalization.Calendar types in the System.Globalization namespace. In addition to the Gregorian calendar, this includes calendars that use different year and month systems, such as the Hjiri calendar.
You can specify whether a Calendar control allows you to select a single day, week, or month by setting the SelectionMode property.
By default, the control displays the headers for each day of the month, days of the week, headings with month names and years, links for selecting days of the month, and links for moving to the next and last month. You can customize the appearance of a Calendar control by setting properties that control the styles in different parts of the control. The following table lists the properties of the styles for the different parts of the specified control.
Attribute description
dayhead Erstyle |
|
daystyle |
|
nextprevstyle |
|
othermonthdaystyle |
|
selecteddaystyle |
|
selectorstyle |
|
titlestyle |
|
todaydaystyle |
|
weekenddaystyle |
|
You can also show or hide different parts of a control. The following table lists the properties that control which parts are displayed or hidden.
ShowDayHeader |
Shows or hides the sections that display the days of the week. |
Showgridlines |
Shows or hides gridlines between days of the month. |
Shownextprevmonth |
Shows or hides navigation controls that point to the next month or last month. |
Showtitle |
Shows or hides the caption section. |
Although the Calendar control does not support binding to a data source, you can modify the contents and formatting of individual date cells. Before displaying the Calendar control on a Web page, it creates and assembles the components that make up the control. The DayRender event is raised when each date cell in the Calendar control is created. By providing code in the event handler for the DayRender event, you can control its content and formatting when you create a date cell.
The Calendar control renders ECMAScript (JScript, JavaScript) to the client browser. The client browser must enable ECMAScript for this control to work correctly.
The following code example demonstrates how to create a Calendar control on a Web page.
Foreground code:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Calendar.aspx.cs" inherits= "Webcontrols_calendar"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Background code:
Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
public partial class WebControls_Calendar:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
lblbendishijian.text = System.DateTime.Now.ToString ();
}
protected void btnSubmit_Click (object sender, EventArgs e)
{
Lblstate.text = CalDate.SelectedDate.ToShortDateString ();
}
Display effect:
Please choose the date: 2013-03-13
Local date and time is: 2013-03-06 10:22:23