Create a schedule using ASP.NET WebForms Calendar Control


This post will show you how to create a scheduling/planning application using ASP.NET Calendar control without using any Nuget. Many paid controls are already available in the market, but you can develop your own quickly with asp.net calendar control.

Many people think that the Calendar control is only used as a date picker control, but the Calendar control can also 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 each day and then add Label controls (or other controls) to the Cell object’s Controls collection DayRender event handler. The following example shows how a Calendar control can be used as a schedule display showing special days.

Let’s create a web page and add a Calendar control on the page. After that, add the following code inside the code behind. We will set the calendar control programmatically and then set the events created

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calender2.aspx.cs" Inherits="Calender2" %>

<!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>Untitled Page</title>
</head>
<body>
 <form id="form1" runat="server">
     <div>
         <asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender" OnPreRender="Calendar1_PreRender"
             OnSelectionChanged="Calendar1_SelectionChanged1" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged1">
         </asp:Calendar>
     </div>
 </form>
</body>
</html>

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)
  {
      GetSchedule();
      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 =
      System.Drawing.Color.WhiteSmoke;
      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 />";
      e.Cell.Controls.Add(lit);
      if (schedule[e.Day.Date.ToShortDateString()] != null)
      {
          Label lbl = new Label();
          lbl.Visible = true;
          lbl.Text = (string)schedule[e.Day.Date.ToShortDateString()];
          e.Cell.Controls.Add(lbl);
      }
  }
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post