How To Create a schedule using Calendar Control

Many people think that the Calendar control is only used as a date picker control, but the Calendar control can also be used to display a schedule. The trick to using the Calendar control to display scheduled items and special days is to make the control large enough to display text in each day, and then add Label controls (or other controls) to the Cell object’s Controls collection in the DayRender event handler. The following example shows how a Calendar control can be used as a schedule display showing special days. In this example, a Web page was created and a Calendar control was added to the page. The following code was added to the code-behind page to show how the Calendar control’s properties can be set programmatically and the Calendar control events can be used.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calender2.aspx.cs" Inherits="Calender2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
 <title>Untitled Page</title>
 <form id="form1" runat="server">
         <asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender" OnPreRender="Calendar1_PreRender"
             OnSelectionChanged="Calendar1_SelectionChanged1" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged1">
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Calender2 : System.Web.UI.Page
  Hashtable schedule = new Hashtable();
  protected void Page_Load(object sender, EventArgs e)
      Calendar1.Style.Add("position", "absolute");
      Calendar1.Style.Add("left", "5px");
      Calendar1.Style.Add("top", "50px");
      Calendar1.Caption = "Special Days";
      Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
      Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth;
      Calendar1.TitleFormat = TitleFormat.MonthYear;
      Calendar1.ShowGridLines = true;
      Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left;
      Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top;
      Calendar1.DayStyle.Height = new Unit(75);
      Calendar1.DayStyle.Width = new Unit(100);
      Calendar1.OtherMonthDayStyle.BackColor =
      Calendar1.TodaysDate = new DateTime(2008, 12, 1);
      Calendar1.VisibleDate = Calendar1.TodaysDate;
  private void GetSchedule()
      schedule["11/23/2008"] = "Thanksgiving";
      schedule["12/5/2008"] = "Birthday";
      schedule["12/16/2008"] = "First day of Chanukah";
      schedule["12/23/2008"] = "Last day of Chanukah";
      schedule["12/24/2008"] = "Christmas Eve";
      schedule["12/25/2008"] = "Christmas";
      schedule["12/26/2008"] = "Boxing Day";
      schedule["12/31/2008"] = "New Year's Eve";
      schedule["1/1/2009"] = "New Year's Day";

  protected void Calendar1_PreRender(object sender, EventArgs e)

  protected void Calendar1_VisibleMonthChanged1(object sender, MonthChangedEventArgs e)
      Response.Write("Month changed to: " + e.NewDate.ToShortDateString());
  protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
      Response.Write("Selection changed to: "
      + Calendar1.SelectedDate.ToShortDateString());
  protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
      Literal lit = new Literal();
      lit.Visible = true;
      lit.Text = "<br />";
      if (schedule[e.Day.Date.ToShortDateString()] != null)
          Label lbl = new Label();
          lbl.Visible = true;
          lbl.Text = (string)schedule[e.Day.Date.ToShortDateString()];

Post a Comment


Close Menu