Calendar control in code behind (C#)
<%@ Page language="c#" src="CalendarTest.aspx.cs" AutoEventWireup="false" Inherits="CalendarTest" %>
<HTML>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:Calendar id="Calendar1" runat="server" SelectionMode="DayWeekMonth" BorderWidth="1px" BackColor="#FFFFCC" Width="220px" DayNameFormat="FirstLetter" ForeColor="#663399" Height="200px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#FFCC66" ShowGridLines="True">
<TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle>
<SelectorStyle BackColor="#FFCC66"></SelectorStyle>
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle>
<DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle>
<SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle>
<TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle>
<OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle>
</asp:Calendar></P>
<P>
<asp:Label id="lbl" runat="server" Width="544px" Height="72px" Font-Size="Small" Font-Names="Verdana" Font-Bold="True">Look for the month of May ...</asp:Label></P>
</form>
</body>
</HTML>
<%-- CalendarTest.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class CalendarTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Calendar Calendar1;
protected System.Web.UI.WebControls.Label lbl;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Calendar1_DayRender(Object source, DayRenderEventArgs e)
{
// Check for May 5 in any year, and format it.
if (e.Day.Date.Day == 5 && e.Day.Date.Month == 5)
{
e.Cell.BackColor = System.Drawing.Color.Yellow;
// Add some static text to the cell.
Label lbl = new Label();
lbl.Text = "<br>My Birthday!";
e.Cell.Controls.Add(lbl);
}
}
}
--%>
Related examples in the same category