2 // System.Web.UI.WebControls.Calendar.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Collections;
34 using System.ComponentModel;
36 using System.Security.Permissions;
37 using System.Threading;
39 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
45 [DefaultEvent("SelectionChanged")]
46 [DefaultProperty("SelectedDate")]
47 [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
49 [ControlValueProperty ("SelectedDate", "1/1/0001 12:00:00 AM")]
50 [SupportsEventValidation]
52 public class Calendar : WebControl, IPostBackEventHandler {
54 private TableItemStyle dayHeaderStyle;
55 private TableItemStyle dayStyle;
56 private TableItemStyle nextPrevStyle;
57 private TableItemStyle otherMonthDayStyle;
58 private TableItemStyle selectedDayStyle;
59 private TableItemStyle titleStyle;
60 private TableItemStyle todayDayStyle;
61 private TableItemStyle selectorStyle;
62 private TableItemStyle weekendDayStyle;
63 private DateTimeFormatInfo dateInfo;
64 private SelectedDatesCollection selectedDatesCollection;
65 private ArrayList dateList;
66 private static DateTime dateZenith = new DateTime (2000, 1,1);
67 private const int daysInAWeek = 7;
68 private static readonly object DayRenderEvent = new object ();
69 private static readonly object SelectionChangedEvent = new object ();
70 private static readonly object VisibleMonthChangedEvent = new object ();
74 dateInfo = Thread.CurrentThread.CurrentUICulture.DateTimeFormat;
80 [WebSysDescription ("")]
81 [WebCategoryAttribute ("Appearance")]
82 public virtual string Caption
85 return ViewState.GetString ("Caption", "");
88 ViewState["Caption"] = value;
92 [DefaultValue (TableCaptionAlign.NotSet)]
93 [WebSysDescription ("")]
94 [WebCategoryAttribute ("Accessibility")]
95 public virtual TableCaptionAlign CaptionAlign
98 return (TableCaptionAlign)ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet);
101 ViewState ["CaptionAlign"] = value;
110 [WebSysDescription ("")]
111 [WebCategory ("Layout")]
112 public int CellPadding {
114 return ViewState.GetInt ("CellPadding", 2);
119 throw new ArgumentOutOfRangeException ("The specified cell padding is less than -1.");
121 ViewState ["CellPadding"] = value;
129 [WebSysDescription ("")]
130 [WebCategory ("Layout")]
131 public int CellSpacing {
133 return ViewState.GetInt ("CellSpacing", 0);
138 throw new ArgumentOutOfRangeException ("The specified cell spacing is less than -1");
140 ViewState ["CellSpacing"] = value;
144 [PersistenceMode(PersistenceMode.InnerProperty)]
145 [NotifyParentProperty(true)]
146 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
147 [WebSysDescription ("")]
148 [WebCategory ("Style")]
149 public TableItemStyle DayHeaderStyle {
151 if (dayHeaderStyle == null) {
152 dayHeaderStyle = new TableItemStyle ();
153 if (IsTrackingViewState)
154 dayHeaderStyle.TrackViewState ();
157 return dayHeaderStyle;
164 [DefaultValue(DayNameFormat.Short)]
165 [WebSysDescription ("")]
166 [WebCategory ("Appearance")]
167 public DayNameFormat DayNameFormat {
169 return (DayNameFormat) ViewState.GetInt ("DayNameFormat", (int) DayNameFormat.Short);
173 if (value != DayNameFormat.FirstLetter && value != DayNameFormat.FirstTwoLetters &&
174 value != DayNameFormat.Full && value != DayNameFormat.Short) {
175 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
178 ViewState ["DayNameFormat"] = value;
183 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
184 [NotifyParentProperty(true)]
185 [PersistenceMode(PersistenceMode.InnerProperty)]
186 [WebSysDescription ("")]
187 [WebCategory ("Style")]
188 public TableItemStyle DayStyle {
190 if (dayStyle == null) {
191 dayStyle = new TableItemStyle ();
192 if (IsTrackingViewState)
193 dayStyle.TrackViewState ();
203 [DefaultValue(FirstDayOfWeek.Default)]
204 [WebSysDescription ("")]
205 [WebCategory ("Appearance")]
206 public FirstDayOfWeek FirstDayOfWeek {
208 return (FirstDayOfWeek) ViewState.GetInt ("FirstDayOfWeek", (int) FirstDayOfWeek.Default);
212 if (value < FirstDayOfWeek.Sunday || value > FirstDayOfWeek.Default) {
213 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
216 ViewState ["FirstDayOfWeek"] = value;
223 [DefaultValue(">")]
227 [WebSysDescription ("")]
228 [WebCategory ("Appearance")]
229 public string NextMonthText {
231 return ViewState.GetString ("NextMonthText", ">");
235 ViewState ["NextMonthText"] = value;
242 [DefaultValue(NextPrevFormat.CustomText)]
243 [WebSysDescription ("")]
244 [WebCategory ("Appearance")]
245 public NextPrevFormat NextPrevFormat {
247 return (NextPrevFormat) ViewState.GetInt ("NextPrevFormat", (int) NextPrevFormat.CustomText);
251 if (value != NextPrevFormat.CustomText && value != NextPrevFormat.ShortMonth && value != NextPrevFormat.FullMonth) {
252 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
255 ViewState ["NextPrevFormat"] = value;
259 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
260 [NotifyParentProperty(true)]
261 [PersistenceMode(PersistenceMode.InnerProperty)]
262 [WebSysDescription ("")]
263 [WebCategory ("Style")]
264 public TableItemStyle NextPrevStyle {
266 if (nextPrevStyle == null) {
267 nextPrevStyle = new TableItemStyle ();
268 if (IsTrackingViewState)
269 nextPrevStyle.TrackViewState ();
272 return nextPrevStyle;
277 [NotifyParentProperty(true)]
278 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
279 [PersistenceMode(PersistenceMode.InnerProperty)]
280 [WebSysDescription ("")]
281 [WebCategory ("Style")]
282 public TableItemStyle OtherMonthDayStyle {
284 if (otherMonthDayStyle == null) {
285 otherMonthDayStyle = new TableItemStyle ();
286 if (IsTrackingViewState)
287 otherMonthDayStyle.TrackViewState ();
290 return otherMonthDayStyle;
297 [DefaultValue("<")]
301 [WebSysDescription ("")]
302 [WebCategory ("Appearance")]
303 public string PrevMonthText {
305 return ViewState.GetString ("PrevMonthText", "<");
309 ViewState ["PrevMonthText"] = value;
314 [Bindable(true, BindingDirection.TwoWay)]
318 [DefaultValue("1/1/0001 12:00:00 AM")]
319 [WebSysDescription ("")]
320 [WebCategory ("Appearance")]
321 public DateTime SelectedDate {
323 if (SelectedDates.Count > 0)
324 return SelectedDates [0];
326 return DateTime.MinValue;
330 SelectedDates.SelectRange (value, value);
335 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
336 [WebSysDescription ("")]
337 [WebCategory ("Appearance")]
338 public SelectedDatesCollection SelectedDates {
340 if (dateList == null)
341 dateList = new ArrayList ();
343 if (selectedDatesCollection == null)
344 selectedDatesCollection = new SelectedDatesCollection (dateList);
346 return selectedDatesCollection;
351 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
352 [NotifyParentProperty(true)]
353 [PersistenceMode(PersistenceMode.InnerProperty)]
354 [WebSysDescription ("")]
355 [WebCategory ("Style")]
356 public TableItemStyle SelectedDayStyle {
358 if (selectedDayStyle == null) {
359 selectedDayStyle = new TableItemStyle ();
360 if (IsTrackingViewState)
361 selectedDayStyle.TrackViewState ();
364 return selectedDayStyle;
371 [DefaultValue(CalendarSelectionMode.Day)]
372 [WebSysDescription ("")]
373 [WebCategory ("Behavior")]
374 public CalendarSelectionMode SelectionMode {
376 return (CalendarSelectionMode) ViewState.GetInt ("SelectionMode", (int) CalendarSelectionMode.Day);
380 if (value != CalendarSelectionMode.Day && value != CalendarSelectionMode.DayWeek &&
381 value != CalendarSelectionMode.DayWeekMonth && value != CalendarSelectionMode.None) {
382 throw new ArgumentOutOfRangeException ("The specified selection mode is not one of the CalendarSelectionMode values.");
384 ViewState ["SelectionMode"] = value;
391 [DefaultValue(">>")]
395 [WebSysDescription ("")]
396 [WebCategory ("Appearance")]
397 public string SelectMonthText {
399 return ViewState.GetString ("SelectMonthText", ">>");
403 ViewState ["SelectMonthText"] = value;
407 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
408 [NotifyParentProperty(true)]
409 [PersistenceMode(PersistenceMode.InnerProperty)]
410 [WebSysDescription ("")]
411 [WebCategory ("Style")]
412 public TableItemStyle SelectorStyle {
414 if (selectorStyle == null) {
415 selectorStyle = new TableItemStyle ();
416 if (IsTrackingViewState)
417 selectorStyle.TrackViewState ();
420 return selectorStyle;
427 [DefaultValue(">")]
431 [WebSysDescription ("")]
432 [WebCategory ("Appearance")]
433 public string SelectWeekText {
435 return ViewState.GetString ("SelectWeekText", ">");
439 ViewState ["SelectWeekText"] = value;
447 [WebSysDescription ("")]
448 [WebCategory ("Appearance")]
449 public bool ShowDayHeader {
451 return ViewState.GetBool ("ShowDayHeader", true);
455 ViewState ["ShowDayHeader"] = value;
462 [DefaultValue(false)]
463 [WebSysDescription ("")]
464 [WebCategory ("Appearance")]
465 public bool ShowGridLines {
467 return ViewState.GetBool ("ShowGridLines", false);
471 ViewState ["ShowGridLines"] = value;
479 [WebSysDescription ("")]
480 [WebCategory ("Appearance")]
481 public bool ShowNextPrevMonth {
483 return ViewState.GetBool ("ShowNextPrevMonth", true);
487 ViewState ["ShowNextPrevMonth"] = value;
495 [WebSysDescription ("")]
496 [WebCategory ("Appearance")]
497 public bool ShowTitle {
499 return ViewState.GetBool ("ShowTitle", true);
503 ViewState ["ShowTitle"] = value;
510 [DefaultValue(TitleFormat.MonthYear)]
511 [WebSysDescription ("")]
512 [WebCategory ("Appearance")]
513 public TitleFormat TitleFormat {
515 return (TitleFormat) ViewState.GetInt ("TitleFormat", (int) TitleFormat.MonthYear);
519 if (value != TitleFormat.Month && value != TitleFormat.MonthYear) {
520 throw new ArgumentOutOfRangeException ("The specified title format is not one of the TitleFormat values.");
523 ViewState ["TitleFormat"] = value;
527 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
528 [NotifyParentProperty(true)]
529 [PersistenceMode(PersistenceMode.InnerProperty)]
530 [WebSysDescription ("")]
531 [WebCategory ("Style")]
532 public TableItemStyle TitleStyle {
534 if (titleStyle == null) {
535 titleStyle = new TableItemStyle ();
536 if (IsTrackingViewState)
537 titleStyle.TrackViewState ();
545 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
546 [NotifyParentProperty(true)]
547 [PersistenceMode(PersistenceMode.InnerProperty)]
548 [WebSysDescription ("")]
549 [WebCategory ("Style")]
550 public TableItemStyle TodayDayStyle {
552 if (todayDayStyle == null) {
553 todayDayStyle = new TableItemStyle ();
554 if (IsTrackingViewState)
555 todayDayStyle.TrackViewState ();
558 return todayDayStyle;
566 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
567 [WebSysDescription ("")]
568 [WebCategory ("Style")]
569 public DateTime TodaysDate {
571 object obj = ViewState ["TodaysDate"];
574 return (DateTime) obj;
576 return DateTime.Today;
580 ViewState ["TodaysDate"] = value.Date;
585 [DefaultValue (true)]
586 [WebSysDescription ("")]
587 [WebCategoryAttribute ("Accessibility")]
588 public virtual bool UseAccessibleHeader
591 return ViewState.GetBool ("UseAccessibleHeader", true);
594 ViewState ["UseAccessibleHeader"] = value;
600 [DefaultValue("1/1/0001 12:00:00 AM")]
601 [WebSysDescription ("")]
602 [WebCategory ("Style")]
603 public DateTime VisibleDate {
605 object obj = ViewState ["VisibleDate"];
608 return (DateTime) obj;
610 return DateTime.MinValue;
614 ViewState ["VisibleDate"] = value.Date;
618 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
619 [NotifyParentProperty(true)]
620 [PersistenceMode(PersistenceMode.InnerProperty)]
621 [WebSysDescription ("")]
622 [WebCategory ("Style")]
623 public TableItemStyle WeekendDayStyle {
625 if (weekendDayStyle == null) {
626 weekendDayStyle = new TableItemStyle ();
627 if (IsTrackingViewState)
628 weekendDayStyle.TrackViewState ();
631 return weekendDayStyle;
636 // Private properties
637 private DateTime DisplayDate {
639 DateTime dateTime = VisibleDate;
640 if (dateTime == DateTime.MinValue) // If visibledate is still the default value
641 dateTime = TodaysDate;
647 private DayOfWeek DisplayFirstDayOfWeek {
649 if (FirstDayOfWeek != FirstDayOfWeek.Default)
650 return (DayOfWeek) FirstDayOfWeek;
652 return (DayOfWeek) dateInfo.FirstDayOfWeek;
656 protected override ControlCollection CreateControlCollection ()
658 return base.CreateControlCollection ();
661 protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)
663 if (selectionMode == CalendarSelectionMode.DayWeek || selectionMode == CalendarSelectionMode.DayWeekMonth)
670 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
672 RaisePostBackEvent (eventArgument);
675 protected virtual void RaisePostBackEvent (string arg)
677 void IPostBackEventHandler.RaisePostBackEvent (string arg)
683 if (arg[0] == 'V') { // Goes to Next or Previous month
684 DateTime prev = VisibleDate;
685 int days = Int32.Parse (arg.Substring (1));
686 DateTime dt = GetGlobalCalendar().AddDays (dateZenith, days);
688 OnVisibleMonthChanged (VisibleDate, prev);
692 if (arg[0] == 'R') { // Selects a range of dates
693 string num, date, days;
694 num = arg.Substring (1);
695 days = num.Substring (num.Length - 2, 2);
696 date = num.Substring (0, num.Length - 2);
697 DateTime d = GetGlobalCalendar().AddDays (dateZenith, Int32.Parse (date));
698 SelectedDates.SelectRange (d, d.AddDays (Int32.Parse (days)));
699 OnSelectionChanged ();
703 // Selects a single day
704 int daysFromZenith = Int32.Parse (arg);
705 DateTime day = GetGlobalCalendar().AddDays (dateZenith, daysFromZenith);
706 SelectedDates.SelectRange (day, day);
707 OnSelectionChanged ();
710 protected override void LoadViewState (object savedState)
712 object [] states = (object []) savedState;
714 if (states [0] != null)
715 base.LoadViewState (states [0]);
717 if (states [1] != null)
718 DayHeaderStyle.LoadViewState (states [1]);
720 if (states [2] != null)
721 DayStyle.LoadViewState (states [2]);
723 if (states [3] != null)
724 NextPrevStyle.LoadViewState (states [3]);
726 if (states [4] != null)
727 OtherMonthDayStyle.LoadViewState (states [4]);
729 if (states [5] != null)
730 SelectedDayStyle.LoadViewState (states [5]);
732 if (states [6] != null)
733 TitleStyle.LoadViewState (states [6]);
735 if (states [7] != null)
736 TodayDayStyle.LoadViewState (states [7]);
738 if (states [8] != null)
739 SelectorStyle.LoadViewState (states [8]);
741 if (states [9] != null)
742 WeekendDayStyle.LoadViewState (states [9]);
744 ArrayList array = (ArrayList) ViewState ["SelectedDates"];
747 selectedDatesCollection = new SelectedDatesCollection (dateList);
751 protected virtual void OnDayRender (TableCell cell, CalendarDay day)
753 DayRenderEventHandler eh = (DayRenderEventHandler) (Events [DayRenderEvent]);
755 eh (this, new DayRenderEventArgs (cell, day));
763 override void OnPreRender (EventArgs e)
765 base.OnPreRender (e);
768 protected virtual void OnSelectionChanged ()
770 EventHandler eh = (EventHandler) (Events [SelectionChangedEvent]);
772 eh (this, EventArgs.Empty);
775 protected virtual void OnVisibleMonthChanged (DateTime newDate, DateTime previousDate)
777 MonthChangedEventHandler eh = (MonthChangedEventHandler) (Events [VisibleMonthChangedEvent]);
779 eh (this, new MonthChangedEventArgs (newDate, previousDate));
787 override void Render (HtmlTextWriter writer)
789 Table table = new Table ();
790 table.CellSpacing = CellSpacing;
791 table.CellPadding = CellPadding;
792 if (ControlStyleCreated)
793 table.ControlStyle.CopyFrom (ControlStyle);
794 table.CopyBaseAttributes (this);
798 table.GridLines = GridLines.Both;
800 table.RenderBeginTag (writer);
804 WriteCaption (writer);
811 WriteDayHeader (writer);
815 table.RenderEndTag (writer);
818 protected override object SaveViewState ()
820 object [] states = new object [10];
822 if (dayHeaderStyle != null)
823 states [1] = dayHeaderStyle.SaveViewState ();
825 if (dayStyle != null)
826 states [2] = dayStyle.SaveViewState ();
828 if (nextPrevStyle != null)
829 states [3] = nextPrevStyle.SaveViewState ();
831 if (otherMonthDayStyle != null)
832 states [4] = otherMonthDayStyle.SaveViewState ();
834 if (selectedDayStyle != null)
835 states [5] = selectedDayStyle.SaveViewState ();
837 if (titleStyle != null)
838 states [6] = titleStyle.SaveViewState ();
840 if (todayDayStyle != null)
841 states [7] =todayDayStyle.SaveViewState ();
843 if (selectorStyle != null)
844 states [8] = selectorStyle.SaveViewState ();
846 if (weekendDayStyle != null)
847 states [9] = weekendDayStyle.SaveViewState ();
849 if (SelectedDates.Count > 0) {
850 ViewState ["SelectedDates"] = dateList;
853 states [0] = base.SaveViewState ();
855 for (int i = 0; i < states.Length; i++) {
856 if (states [i] != null) {
864 protected override void TrackViewState ()
866 base.TrackViewState ();
868 if (dayHeaderStyle != null)
869 dayHeaderStyle.TrackViewState ();
871 if (dayStyle != null)
872 dayStyle.TrackViewState ();
874 if (nextPrevStyle != null)
875 nextPrevStyle.TrackViewState ();
877 if (otherMonthDayStyle != null)
878 otherMonthDayStyle.TrackViewState ();
880 if (selectedDayStyle != null)
881 selectedDayStyle.TrackViewState ();
883 if (titleStyle != null)
884 titleStyle.TrackViewState ();
886 if (todayDayStyle != null)
887 todayDayStyle.TrackViewState ();
889 if (selectorStyle != null)
890 selectorStyle.TrackViewState ();
892 if (weekendDayStyle != null)
893 weekendDayStyle.TrackViewState ();
899 private void WriteDayHeader (HtmlTextWriter writer)
903 i = first = (int) (DisplayFirstDayOfWeek);
907 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
909 if (SelectionMode == CalendarSelectionMode.DayWeek) {
910 cell = new TableCell();
911 cell.HorizontalAlign = HorizontalAlign.Center;
912 cell.ApplyStyle (DayHeaderStyle);
915 cell.RenderBeginTag (writer);
916 cell.RenderEndTag (writer);
918 if (SelectionMode == CalendarSelectionMode.DayWeekMonth) {
919 TableCell selector = new TableCell ();
920 selector.ApplyStyle (SelectorStyle);
921 selector.HorizontalAlign = HorizontalAlign.Center;
923 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
924 int days = DateTime.DaysInMonth (DisplayDate.Year, DisplayDate.Month);
926 selector.RenderBeginTag (writer);
927 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + days, SelectMonthText, DayHeaderStyle.ForeColor, Enabled));
928 selector.RenderEndTag (writer);
933 DayOfWeek dayOfWeek = (DayOfWeek) i;
934 dayName = dateInfo.GetDayName (dayOfWeek);
937 if (UseAccessibleHeader) {
938 writer.AddAttribute (HtmlTextWriterAttribute.Abbr, dayName);
939 writer.AddAttribute (HtmlTextWriterAttribute.Scope, "col");
940 cell = new TableHeaderCell();
944 cell = new TableCell();
946 cell.HorizontalAlign = HorizontalAlign.Center;
947 cell.ApplyStyle (DayHeaderStyle);
949 cell.RenderBeginTag (writer);
951 switch (DayNameFormat) {
952 case DayNameFormat.FirstLetter:
953 dayName = dayName.Substring (0, 1);
955 case DayNameFormat.FirstTwoLetters:
956 dayName = dayName.Substring (0, 2);
958 case DayNameFormat.Full:
960 case DayNameFormat.Short:
962 dayName = dateInfo.GetAbbreviatedDayName (dayOfWeek);
966 writer.Write (dayName);
967 cell.RenderEndTag (writer);
969 if (i >= daysInAWeek - 1) {
979 writer.RenderEndTag ();
982 private void WriteDay (DateTime date, HtmlTextWriter writer)
984 Style style = new Style ();
985 TableCell cell = new TableCell ();
987 CalendarDay day = new CalendarDay (date,
988 IsWeekEnd (date.DayOfWeek),
989 date == TodaysDate, SelectedDates.Contains (date),
990 GetGlobalCalendar ().GetMonth (DisplayDate) != GetGlobalCalendar ().GetMonth (date),
991 date.Day.ToString ());
993 day.IsSelectable = SelectionMode != CalendarSelectionMode.None;
994 cell.HorizontalAlign = HorizontalAlign.Center;
995 cell.Width = Unit.Percentage (GetCellWidth ());
997 LiteralControl lit = new LiteralControl (day.DayNumberText);
998 cell.Controls.Add (lit);
1000 OnDayRender (cell, day);
1002 if (dayStyle != null && !dayStyle.IsEmpty) {
1003 style.CopyFrom (dayStyle);
1006 if (day.IsWeekend && weekendDayStyle != null && !weekendDayStyle.IsEmpty) {
1007 style.CopyFrom (weekendDayStyle);
1010 if (day.IsToday && todayDayStyle != null && !todayDayStyle.IsEmpty) {
1011 style.CopyFrom (todayDayStyle);
1014 if (day.IsOtherMonth && otherMonthDayStyle != null && !otherMonthDayStyle.IsEmpty) {
1015 style.CopyFrom (otherMonthDayStyle);
1018 if (day.IsSelected && Enabled) {
1019 style.BackColor = Color.Silver;
1020 style.ForeColor = Color.White;
1021 if (selectedDayStyle != null && !selectedDayStyle.IsEmpty) {
1022 style.CopyFrom (selectedDayStyle);
1026 cell.ApplyStyle (style);
1028 lit.Text = BuildLink (GetDaysFromZenith (date).ToString (), day.DayNumberText,
1029 cell.ForeColor, day.IsSelectable && Enabled);
1031 cell.RenderControl (writer);
1034 private void WriteDays (HtmlTextWriter writer)
1036 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
1038 TableCell selectorCell = null;
1041 // Goes backwards until we find the date of that is begining of the week
1042 for (n = 0; n < daysInAWeek; n++) {
1043 if (date.DayOfWeek == DisplayFirstDayOfWeek)
1046 date = GetGlobalCalendar().AddDays (date, -1);
1048 /* if the start date is the first day of the week, we need to shift backward one more week */
1050 date = GetGlobalCalendar().AddDays (date, -1 * daysInAWeek);
1052 lastDate = GetGlobalCalendar().AddDays (date, 6 * daysInAWeek); // Always six weeks per months
1055 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1057 if (HasWeekSelectors (SelectionMode)) { // Week selector
1058 if (selectorCell == null) {
1059 selectorCell = new TableCell ();
1060 selectorCell.ApplyStyle (SelectorStyle);
1061 selectorCell.HorizontalAlign = HorizontalAlign.Center;
1062 selectorCell.Width = Unit.Percentage (GetCellWidth ());
1065 selectorCell.RenderBeginTag (writer);
1066 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + "07", SelectWeekText, selectorCell.ForeColor, Enabled));
1067 selectorCell.RenderEndTag (writer);
1070 for (int i = 0; i < daysInAWeek; i++) {
1071 WriteDay (date, writer);
1072 date = GetGlobalCalendar().AddDays (date, 1);
1075 writer.RenderEndTag ();
1076 if (date >= lastDate)
1081 private string BuildLink (string arg, string text, Color foreColor, bool hasLink)
1083 string str = string.Empty;
1085 hasLink = (Page != null && hasLink == true) ? true : false;
1089 str += Page.ClientScript.GetPostBackClientHyperlink (this, arg);
1093 if (!foreColor.IsEmpty) {
1096 if (ForeColor.IsEmpty)
1102 str += " style=color:" + ColorTranslator.ToHtml (clr);
1113 private int GetDaysFromZenith (DateTime date)
1115 TimeSpan span = date.Subtract (dateZenith);
1120 void WriteCaption (HtmlTextWriter writer)
1122 if (CaptionAlign != TableCaptionAlign.NotSet)
1123 writer.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString (CultureInfo.InvariantCulture));
1125 writer.RenderBeginTag (HtmlTextWriterTag.Caption);
1126 writer.Write (Caption);
1127 writer.RenderEndTag ();
1131 private void WriteTitle (HtmlTextWriter writer)
1133 TableCell cellNextPrev = null;
1134 TableCell titleCell = new TableCell ();
1135 Table tableTitle = new Table ();
1137 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1139 titleCell.ColumnSpan = HasWeekSelectors (SelectionMode) ? 8 : 7;
1141 if (titleStyle != null && !titleStyle.IsEmpty && !titleStyle.BackColor.IsEmpty) {
1142 titleCell.BackColor = titleStyle.BackColor;
1144 titleCell.BackColor = Color.Silver;
1147 titleCell.RenderBeginTag (writer);
1150 tableTitle.Width = Unit.Percentage (100);
1151 if (titleStyle != null && !titleStyle.IsEmpty) {
1152 tableTitle.ApplyStyle (titleStyle);
1155 tableTitle.RenderBeginTag (writer);
1156 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1158 if (ShowNextPrevMonth) { // Previous Table Data
1159 cellNextPrev = new TableCell ();
1160 cellNextPrev.ApplyStyle (nextPrevStyle);
1161 cellNextPrev.Width = Unit.Percentage (15);
1163 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, - 1);
1164 date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
1165 cellNextPrev.RenderBeginTag (writer);
1166 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, false), cellNextPrev.ForeColor, Enabled));
1167 cellNextPrev.RenderEndTag (writer);
1170 // Current Month Table Data
1173 TableCell cellMonth = new TableCell ();
1174 cellMonth.Width = Unit.Percentage (70);
1175 cellMonth.HorizontalAlign = HorizontalAlign.Center;
1177 cellMonth.RenderBeginTag (writer);
1179 str = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (DisplayDate));
1181 if (TitleFormat == TitleFormat.MonthYear)
1182 str += " " + (DisplayDate.Year.ToString ());
1185 cellMonth.RenderEndTag (writer);
1188 if (ShowNextPrevMonth) { // Next Table Data
1189 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, + 1);
1190 date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
1192 cellNextPrev.HorizontalAlign = HorizontalAlign.Right;
1193 cellNextPrev.RenderBeginTag (writer);
1194 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, true), cellNextPrev.ForeColor, Enabled));
1195 cellNextPrev.RenderEndTag (writer);
1198 writer.RenderEndTag ();
1199 tableTitle.RenderEndTag (writer);
1200 titleCell.RenderEndTag (writer);
1201 writer.RenderEndTag (); //tr
1206 private string GetNextPrevFormatText (DateTime date, bool next)
1209 switch (NextPrevFormat) {
1210 case NextPrevFormat.FullMonth:
1211 text = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (date));
1213 case NextPrevFormat.ShortMonth:
1214 text = dateInfo.GetAbbreviatedMonthName (GetGlobalCalendar ().GetMonth (date));
1216 case NextPrevFormat.CustomText:
1218 text = ((next) ? NextMonthText : PrevMonthText);
1225 private bool IsWeekEnd (DayOfWeek day)
1227 return (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday);
1230 private double GetCellWidth ()
1232 return HasWeekSelectors (SelectionMode) ? 100/8 : 100/7;
1235 private System.Globalization.Calendar GetGlobalCalendar ()
1237 return DateTimeFormatInfo.CurrentInfo.Calendar;
1240 [WebSysDescription ("")]
1241 [WebCategory ("Action")]
1242 public event DayRenderEventHandler DayRender {
1244 Events.AddHandler (DayRenderEvent, value);
1247 Events.RemoveHandler (DayRenderEvent, value);
1251 [WebSysDescription ("")]
1252 [WebCategory ("Action")]
1253 public event EventHandler SelectionChanged {
1255 Events.AddHandler (SelectionChangedEvent, value);
1258 Events.RemoveHandler (SelectionChangedEvent, value);
1262 [WebSysDescription ("")]
1263 [WebCategory ("Action")]
1264 public event MonthChangedEventHandler VisibleMonthChanged {
1266 Events.AddHandler (VisibleMonthChangedEvent, value);
1269 Events.RemoveHandler (VisibleMonthChangedEvent, value);