ASP.Net - Custom Calendar Control

This simple code snippet shows how to create a custom calendar control using a Web User Control.

  1. // Default.aspx
  2.  
  3. <%@ Register Src="~/xCalendar.ascx" TagName="xCalendar" TagPrefix="x" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head runat="server">
  8.     <title>Custom Calendar</title>
  9.     <style>
  10.         .today { background-color:#ccc; }
  11.     </style>
  12. </head>
  13. <body>
  14.     <form id="form1" runat="server">
  15.     <div>
  16.         <x:xCalendar ID="Calendar" TodayCssClass="today" DayTitleFormat="FullTitle" runat="server" />
  17.     </div>
  18.     </form>
  19. </body>
  20. </html>
  21.  
  22. //xCalendar.ascx
  23.  
  24. <%@ Import Namespace="System.Text" %>
  25. <%@ Control Language="C#" ClassName="xCalendar" %>
  26.  
  27. <script language="c#" runat="server">
  28.  
  29.     private DateTime _Date = DateTime.Now;
  30.     private TitleFormat _TitleFormat = TitleFormat.FullTitle;
  31.    
  32.     public DateTime SelectDate
  33.     {
  34.         set { this._Date = value; }
  35.         get { return this._Date; }
  36.     }
  37.  
  38.     public TitleFormat DayTitleFormat
  39.     {
  40.         set { this._TitleFormat = value; }
  41.         get { return this._TitleFormat; }
  42.     }
  43.  
  44.     public string TodayCssClass
  45.     {
  46.         set;
  47.         get;
  48.     }
  49.    
  50.     protected override void  Render(HtmlTextWriter writer)  
  51.     {
  52.         base.Render(writer);
  53.  
  54.         StringBuilder Control = new StringBuilder();
  55.  
  56.         int DaysInMonth = DateTime.DaysInMonth(SelectDate.Year, SelectDate.Month);
  57.         string FirstDayOfMonth = DateTime.Parse(SelectDate.Year + "-" + SelectDate.Month + "-01").DayOfWeek.ToString();
  58.         int DayWeekNum = 0;
  59.            
  60.         string[] Days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  61.  
  62.         Control.Append("<table border=1><tr>");
  63.        
  64.         for (int i =0; i < Days.Length; i++)
  65.         {
  66.             if (this.DayTitleFormat == TitleFormat.FullTitle)
  67.             {
  68.                 Control.Append("<td>" + Days[i] + "</td>");
  69.             }
  70.             else if (this.DayTitleFormat == TitleFormat.ShortTitle)
  71.             {
  72.                 Control.Append("<td>" + Days[i].Substring(0, 3) + "</td>");
  73.             }
  74.             else
  75.             {
  76.                 Control.Append("<td>" + Days[i].Substring(0, 1) + "</td>");
  77.             }
  78.  
  79.             if (Days[i] == FirstDayOfMonth)
  80.             {
  81.                 DayWeekNum = i;
  82.             }
  83.         }
  84.  
  85.         Control.Append("</tr><tr>");
  86.        
  87.         for (int i =0; i < DayWeekNum; i++)
  88.         {
  89.             Control.Append("<td>&nbsp;</td>");
  90.         }
  91.  
  92.         for (int i = 1; i <= DaysInMonth; i++)
  93.         {
  94.             string strTodayCssClass = "";
  95.             if (SelectDate.Day == i)
  96.             {
  97.                 strTodayCssClass = " class=\"" + TodayCssClass + "\"";
  98.             }
  99.            
  100.             if (DayWeekNum < 7)
  101.             {
  102.                 Control.Append("<td" + strTodayCssClass + ">" + i + "</td>");
  103.                 DayWeekNum++;
  104.             }
  105.             else
  106.             {
  107.                 Control.Append("</tr><td" + strTodayCssClass + ">" + i + "</td>");
  108.                 DayWeekNum = 1;
  109.             }
  110.         }
  111.  
  112.  
  113.         for (int i = 0; i < (7-DayWeekNum); i++)
  114.         {
  115.             Control.Append("<td>&nbsp;</td>");
  116.         }
  117.        
  118.         Control.Append("</tr></table>");
  119.         writer.Write(Control.ToString());
  120.     }
  121.  
  122.     public enum TitleFormat
  123.     {
  124.         FullTitle,
  125.         ShortTitle,
  126.         SingleCharTitle
  127.     }
  128. </script>

No comments:

Post a Comment