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 using System.Web.Util;
41 namespace System.Web.UI.WebControls {
43 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
47 [DefaultEvent("SelectionChanged")]
48 [DefaultProperty("SelectedDate")]
49 [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
51 [ControlValueProperty ("SelectedDate", "1/1/0001 12:00:00 AM")]
52 [SupportsEventValidation]
54 public class Calendar : WebControl, IPostBackEventHandler
56 TableItemStyle dayHeaderStyle;
57 TableItemStyle dayStyle;
58 TableItemStyle nextPrevStyle;
59 TableItemStyle otherMonthDayStyle;
60 TableItemStyle selectedDayStyle;
61 TableItemStyle titleStyle;
62 TableItemStyle todayDayStyle;
63 TableItemStyle selectorStyle;
64 TableItemStyle weekendDayStyle;
65 DateTimeFormatInfo dateInfo;
66 SelectedDatesCollection selectedDatesCollection;
68 DateTime today = DateTime.Today;
69 static DateTime dateZenith = new DateTime (2000, 1,1);
70 const int daysInAWeek = 7;
71 static readonly object DayRenderEvent = new object ();
72 static readonly object SelectionChangedEvent = new object ();
73 static readonly object VisibleMonthChangedEvent = new object ();
82 [WebSysDescription ("")]
83 [WebCategoryAttribute ("Appearance")]
84 public virtual string Caption
87 return ViewState.GetString ("Caption", "");
90 ViewState["Caption"] = value;
94 [DefaultValue (TableCaptionAlign.NotSet)]
95 [WebSysDescription ("")]
96 [WebCategoryAttribute ("Accessibility")]
97 public virtual TableCaptionAlign CaptionAlign
100 return (TableCaptionAlign)ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet);
103 ViewState ["CaptionAlign"] = value;
112 [WebSysDescription ("")]
113 [WebCategory ("Layout")]
114 public int CellPadding {
116 return ViewState.GetInt ("CellPadding", 2);
121 throw new ArgumentOutOfRangeException ("The specified cell padding is less than -1.");
123 ViewState ["CellPadding"] = value;
131 [WebSysDescription ("")]
132 [WebCategory ("Layout")]
133 public int CellSpacing {
135 return ViewState.GetInt ("CellSpacing", 0);
140 throw new ArgumentOutOfRangeException ("The specified cell spacing is less than -1");
142 ViewState ["CellSpacing"] = value;
146 [PersistenceMode(PersistenceMode.InnerProperty)]
147 [NotifyParentProperty(true)]
148 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
149 [WebSysDescription ("")]
150 [WebCategory ("Style")]
151 public TableItemStyle DayHeaderStyle {
153 if (dayHeaderStyle == null) {
154 dayHeaderStyle = new TableItemStyle ();
155 if (IsTrackingViewState)
156 dayHeaderStyle.TrackViewState ();
159 return dayHeaderStyle;
166 [DefaultValue(DayNameFormat.Short)]
167 [WebSysDescription ("")]
168 [WebCategory ("Appearance")]
169 public DayNameFormat DayNameFormat {
171 return (DayNameFormat) ViewState.GetInt ("DayNameFormat", (int) DayNameFormat.Short);
175 if (value != DayNameFormat.FirstLetter && value != DayNameFormat.FirstTwoLetters &&
176 value != DayNameFormat.Full && value != DayNameFormat.Short && value != DayNameFormat.Shortest) {
177 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
180 ViewState ["DayNameFormat"] = value;
185 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
186 [NotifyParentProperty(true)]
187 [PersistenceMode(PersistenceMode.InnerProperty)]
188 [WebSysDescription ("")]
189 [WebCategory ("Style")]
190 public TableItemStyle DayStyle {
192 if (dayStyle == null) {
193 dayStyle = new TableItemStyle ();
194 if (IsTrackingViewState)
195 dayStyle.TrackViewState ();
205 [DefaultValue(FirstDayOfWeek.Default)]
206 [WebSysDescription ("")]
207 [WebCategory ("Appearance")]
208 public FirstDayOfWeek FirstDayOfWeek {
210 return (FirstDayOfWeek) ViewState.GetInt ("FirstDayOfWeek", (int) FirstDayOfWeek.Default);
214 if (value < FirstDayOfWeek.Sunday || value > FirstDayOfWeek.Default) {
215 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
218 ViewState ["FirstDayOfWeek"] = value;
225 [DefaultValue(">")]
229 [WebSysDescription ("")]
230 [WebCategory ("Appearance")]
231 public string NextMonthText {
233 return ViewState.GetString ("NextMonthText", ">");
237 ViewState ["NextMonthText"] = value;
244 [DefaultValue(NextPrevFormat.CustomText)]
245 [WebSysDescription ("")]
246 [WebCategory ("Appearance")]
247 public NextPrevFormat NextPrevFormat {
249 return (NextPrevFormat) ViewState.GetInt ("NextPrevFormat", (int) NextPrevFormat.CustomText);
253 if (value != NextPrevFormat.CustomText && value != NextPrevFormat.ShortMonth && value != NextPrevFormat.FullMonth) {
254 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
257 ViewState ["NextPrevFormat"] = value;
261 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
262 [NotifyParentProperty(true)]
263 [PersistenceMode(PersistenceMode.InnerProperty)]
264 [WebSysDescription ("")]
265 [WebCategory ("Style")]
266 public TableItemStyle NextPrevStyle {
268 if (nextPrevStyle == null) {
269 nextPrevStyle = new TableItemStyle ();
270 if (IsTrackingViewState)
271 nextPrevStyle.TrackViewState ();
274 return nextPrevStyle;
279 [NotifyParentProperty(true)]
280 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
281 [PersistenceMode(PersistenceMode.InnerProperty)]
282 [WebSysDescription ("")]
283 [WebCategory ("Style")]
284 public TableItemStyle OtherMonthDayStyle {
286 if (otherMonthDayStyle == null) {
287 otherMonthDayStyle = new TableItemStyle ();
288 if (IsTrackingViewState)
289 otherMonthDayStyle.TrackViewState ();
292 return otherMonthDayStyle;
299 [DefaultValue("<")]
303 [WebSysDescription ("")]
304 [WebCategory ("Appearance")]
305 public string PrevMonthText {
307 return ViewState.GetString ("PrevMonthText", "<");
311 ViewState ["PrevMonthText"] = value;
316 [Bindable(true, BindingDirection.TwoWay)]
320 [DefaultValue("1/1/0001 12:00:00 AM")]
321 [WebSysDescription ("")]
322 [WebCategory ("Appearance")]
323 public DateTime SelectedDate {
325 if (SelectedDates.Count > 0)
326 return SelectedDates [0];
328 return DateTime.MinValue;
332 SelectedDates.SelectRange (value, value);
337 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
338 [WebSysDescription ("")]
339 [WebCategory ("Appearance")]
340 public SelectedDatesCollection SelectedDates {
342 if (dateList == null)
343 dateList = new ArrayList ();
345 if (selectedDatesCollection == null)
346 selectedDatesCollection = new SelectedDatesCollection (dateList);
348 return selectedDatesCollection;
353 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
354 [NotifyParentProperty(true)]
355 [PersistenceMode(PersistenceMode.InnerProperty)]
356 [WebSysDescription ("")]
357 [WebCategory ("Style")]
358 public TableItemStyle SelectedDayStyle {
360 if (selectedDayStyle == null) {
361 selectedDayStyle = new TableItemStyle ();
362 if (IsTrackingViewState)
363 selectedDayStyle.TrackViewState ();
366 return selectedDayStyle;
373 [DefaultValue(CalendarSelectionMode.Day)]
374 [WebSysDescription ("")]
375 [WebCategory ("Behavior")]
376 public CalendarSelectionMode SelectionMode {
378 return (CalendarSelectionMode) ViewState.GetInt ("SelectionMode", (int) CalendarSelectionMode.Day);
382 if (value != CalendarSelectionMode.Day && value != CalendarSelectionMode.DayWeek &&
383 value != CalendarSelectionMode.DayWeekMonth && value != CalendarSelectionMode.None) {
384 throw new ArgumentOutOfRangeException ("The specified selection mode is not one of the CalendarSelectionMode values.");
386 ViewState ["SelectionMode"] = value;
393 [DefaultValue(">>")]
397 [WebSysDescription ("")]
398 [WebCategory ("Appearance")]
399 public string SelectMonthText {
401 return ViewState.GetString ("SelectMonthText", ">>");
405 ViewState ["SelectMonthText"] = value;
409 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
410 [NotifyParentProperty(true)]
411 [PersistenceMode(PersistenceMode.InnerProperty)]
412 [WebSysDescription ("")]
413 [WebCategory ("Style")]
414 public TableItemStyle SelectorStyle {
416 if (selectorStyle == null) {
417 selectorStyle = new TableItemStyle ();
418 if (IsTrackingViewState)
419 selectorStyle.TrackViewState ();
422 return selectorStyle;
429 [DefaultValue(">")]
433 [WebSysDescription ("")]
434 [WebCategory ("Appearance")]
435 public string SelectWeekText {
437 return ViewState.GetString ("SelectWeekText", ">");
441 ViewState ["SelectWeekText"] = value;
449 [WebSysDescription ("")]
450 [WebCategory ("Appearance")]
451 public bool ShowDayHeader {
453 return ViewState.GetBool ("ShowDayHeader", true);
457 ViewState ["ShowDayHeader"] = value;
464 [DefaultValue(false)]
465 [WebSysDescription ("")]
466 [WebCategory ("Appearance")]
467 public bool ShowGridLines {
469 return ViewState.GetBool ("ShowGridLines", false);
473 ViewState ["ShowGridLines"] = value;
481 [WebSysDescription ("")]
482 [WebCategory ("Appearance")]
483 public bool ShowNextPrevMonth {
485 return ViewState.GetBool ("ShowNextPrevMonth", true);
489 ViewState ["ShowNextPrevMonth"] = value;
497 [WebSysDescription ("")]
498 [WebCategory ("Appearance")]
499 public bool ShowTitle {
501 return ViewState.GetBool ("ShowTitle", true);
505 ViewState ["ShowTitle"] = value;
512 [DefaultValue(TitleFormat.MonthYear)]
513 [WebSysDescription ("")]
514 [WebCategory ("Appearance")]
515 public TitleFormat TitleFormat {
517 return (TitleFormat) ViewState.GetInt ("TitleFormat", (int) TitleFormat.MonthYear);
521 if (value != TitleFormat.Month && value != TitleFormat.MonthYear) {
522 throw new ArgumentOutOfRangeException ("The specified title format is not one of the TitleFormat values.");
525 ViewState ["TitleFormat"] = value;
529 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
530 [NotifyParentProperty(true)]
531 [PersistenceMode(PersistenceMode.InnerProperty)]
532 [WebSysDescription ("")]
533 [WebCategory ("Style")]
534 public TableItemStyle TitleStyle {
536 if (titleStyle == null) {
537 titleStyle = new TableItemStyle ();
538 if (IsTrackingViewState)
539 titleStyle.TrackViewState ();
547 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
548 [NotifyParentProperty(true)]
549 [PersistenceMode(PersistenceMode.InnerProperty)]
550 [WebSysDescription ("")]
551 [WebCategory ("Style")]
552 public TableItemStyle TodayDayStyle {
554 if (todayDayStyle == null) {
555 todayDayStyle = new TableItemStyle ();
556 if (IsTrackingViewState)
557 todayDayStyle.TrackViewState ();
560 return todayDayStyle;
568 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
569 [WebSysDescription ("")]
570 [WebCategory ("Style")]
571 public DateTime TodaysDate {
573 object obj = ViewState ["TodaysDate"];
576 return (DateTime) obj;
582 ViewState ["TodaysDate"] = value.Date;
587 [DefaultValue (true)]
588 [WebSysDescription ("")]
589 [WebCategoryAttribute ("Accessibility")]
590 public virtual bool UseAccessibleHeader
593 return ViewState.GetBool ("UseAccessibleHeader", true);
596 ViewState ["UseAccessibleHeader"] = value;
602 [DefaultValue("1/1/0001 12:00:00 AM")]
603 [WebSysDescription ("")]
604 [WebCategory ("Style")]
605 public DateTime VisibleDate {
607 object obj = ViewState ["VisibleDate"];
610 return (DateTime) obj;
612 return DateTime.MinValue;
616 ViewState ["VisibleDate"] = value.Date;
620 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
621 [NotifyParentProperty(true)]
622 [PersistenceMode(PersistenceMode.InnerProperty)]
623 [WebSysDescription ("")]
624 [WebCategory ("Style")]
625 public TableItemStyle WeekendDayStyle {
627 if (weekendDayStyle == null) {
628 weekendDayStyle = new TableItemStyle ();
629 if (IsTrackingViewState)
630 weekendDayStyle.TrackViewState ();
633 return weekendDayStyle;
638 // Private properties
639 DateTimeFormatInfo DateInfo {
641 if (dateInfo == null)
642 dateInfo = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
648 DateTime DisplayDate {
650 DateTime dateTime = VisibleDate;
651 if (dateTime == DateTime.MinValue) // If visibledate is still the default value
652 dateTime = TodaysDate;
658 DayOfWeek DisplayFirstDayOfWeek {
660 if (FirstDayOfWeek != FirstDayOfWeek.Default)
661 return (DayOfWeek) FirstDayOfWeek;
663 return (DayOfWeek) DateInfo.FirstDayOfWeek;
667 protected override ControlCollection CreateControlCollection ()
669 return base.CreateControlCollection ();
672 protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)
674 if (selectionMode == CalendarSelectionMode.DayWeek || selectionMode == CalendarSelectionMode.DayWeekMonth)
681 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
683 RaisePostBackEvent (eventArgument);
686 protected virtual void RaisePostBackEvent (string arg)
688 void IPostBackEventHandler.RaisePostBackEvent (string arg)
692 ValidateEvent (UniqueID, arg);
697 if (arg[0] == 'V') { // Goes to Next or Previous month
698 DateTime prev = VisibleDate;
699 int days = Int32.Parse (arg.Substring (1));
700 DateTime dt = GetGlobalCalendar().AddDays (dateZenith, days);
702 OnVisibleMonthChanged (VisibleDate, prev);
706 if (arg[0] == 'R') { // Selects a range of dates
707 string num, date, days;
708 num = arg.Substring (1);
709 days = num.Substring (num.Length - 2, 2);
710 date = num.Substring (0, num.Length - 2);
711 DateTime d = GetGlobalCalendar().AddDays (dateZenith, Int32.Parse (date));
712 SelectedDates.SelectRange (d, d.AddDays (Int32.Parse (days)));
713 OnSelectionChanged ();
717 // Selects a single day
718 int daysFromZenith = Int32.Parse (arg);
719 DateTime day = GetGlobalCalendar().AddDays (dateZenith, daysFromZenith);
720 SelectedDates.SelectRange (day, day);
721 OnSelectionChanged ();
724 protected override void LoadViewState (object savedState)
726 object [] states = (object []) savedState;
728 if (states [0] != null)
729 base.LoadViewState (states [0]);
731 if (states [1] != null)
732 DayHeaderStyle.LoadViewState (states [1]);
734 if (states [2] != null)
735 DayStyle.LoadViewState (states [2]);
737 if (states [3] != null)
738 NextPrevStyle.LoadViewState (states [3]);
740 if (states [4] != null)
741 OtherMonthDayStyle.LoadViewState (states [4]);
743 if (states [5] != null)
744 SelectedDayStyle.LoadViewState (states [5]);
746 if (states [6] != null)
747 TitleStyle.LoadViewState (states [6]);
749 if (states [7] != null)
750 TodayDayStyle.LoadViewState (states [7]);
752 if (states [8] != null)
753 SelectorStyle.LoadViewState (states [8]);
755 if (states [9] != null)
756 WeekendDayStyle.LoadViewState (states [9]);
758 ArrayList array = (ArrayList) ViewState ["SelectedDates"];
761 selectedDatesCollection = new SelectedDatesCollection (dateList);
765 protected virtual void OnDayRender (TableCell cell, CalendarDay day)
767 DayRenderEventHandler eh = (DayRenderEventHandler) (Events [DayRenderEvent]);
771 eh (this, new DayRenderEventArgs (cell, day, Page.ClientScript.GetPostBackClientHyperlink (this, GetDaysFromZenith (day.Date).ToString (), true)));
774 eh (this, new DayRenderEventArgs (cell, day));
782 override void OnPreRender (EventArgs e)
784 base.OnPreRender (e);
787 protected virtual void OnSelectionChanged ()
789 EventHandler eh = (EventHandler) (Events [SelectionChangedEvent]);
791 eh (this, EventArgs.Empty);
794 protected virtual void OnVisibleMonthChanged (DateTime newDate, DateTime previousDate)
796 MonthChangedEventHandler eh = (MonthChangedEventHandler) (Events [VisibleMonthChangedEvent]);
798 eh (this, new MonthChangedEventArgs (newDate, previousDate));
806 override void Render (HtmlTextWriter writer)
808 TableStyle ts = new TableStyle ();
809 ts.CellSpacing = CellSpacing;
810 ts.CellPadding = CellPadding;
812 if (ControlStyleCreated)
813 ts.CopyFrom (ControlStyle);
815 ts.GridLines = GridLines.Both;
816 ts.AddAttributesToRender (writer);
817 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
818 writer.RenderBeginTag (HtmlTextWriterTag.Table);
822 WriteCaption (writer);
829 WriteDayHeader (writer);
833 writer.RenderEndTag ();
836 protected override object SaveViewState ()
838 object [] states = new object [10];
840 if (dayHeaderStyle != null)
841 states [1] = dayHeaderStyle.SaveViewState ();
843 if (dayStyle != null)
844 states [2] = dayStyle.SaveViewState ();
846 if (nextPrevStyle != null)
847 states [3] = nextPrevStyle.SaveViewState ();
849 if (otherMonthDayStyle != null)
850 states [4] = otherMonthDayStyle.SaveViewState ();
852 if (selectedDayStyle != null)
853 states [5] = selectedDayStyle.SaveViewState ();
855 if (titleStyle != null)
856 states [6] = titleStyle.SaveViewState ();
858 if (todayDayStyle != null)
859 states [7] =todayDayStyle.SaveViewState ();
861 if (selectorStyle != null)
862 states [8] = selectorStyle.SaveViewState ();
864 if (weekendDayStyle != null)
865 states [9] = weekendDayStyle.SaveViewState ();
867 if (SelectedDates.Count > 0) {
868 ViewState ["SelectedDates"] = dateList;
871 states [0] = base.SaveViewState ();
873 for (int i = 0; i < states.Length; i++) {
874 if (states [i] != null) {
882 protected override void TrackViewState ()
884 base.TrackViewState ();
886 if (dayHeaderStyle != null)
887 dayHeaderStyle.TrackViewState ();
889 if (dayStyle != null)
890 dayStyle.TrackViewState ();
892 if (nextPrevStyle != null)
893 nextPrevStyle.TrackViewState ();
895 if (otherMonthDayStyle != null)
896 otherMonthDayStyle.TrackViewState ();
898 if (selectedDayStyle != null)
899 selectedDayStyle.TrackViewState ();
901 if (titleStyle != null)
902 titleStyle.TrackViewState ();
904 if (todayDayStyle != null)
905 todayDayStyle.TrackViewState ();
907 if (selectorStyle != null)
908 selectorStyle.TrackViewState ();
910 if (weekendDayStyle != null)
911 weekendDayStyle.TrackViewState ();
917 void WriteDayHeader (HtmlTextWriter writer)
921 i = first = (int) (DisplayFirstDayOfWeek);
925 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
927 if (SelectionMode == CalendarSelectionMode.DayWeek) {
928 cell = new TableCell();
929 cell.HorizontalAlign = HorizontalAlign.Center;
930 cell.ApplyStyle (DayHeaderStyle);
933 cell.RenderBeginTag (writer);
934 cell.RenderEndTag (writer);
936 if (SelectionMode == CalendarSelectionMode.DayWeekMonth) {
937 TableCell selector = new TableCell ();
938 selector.ApplyStyle (SelectorStyle);
939 selector.HorizontalAlign = HorizontalAlign.Center;
941 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
942 int days = DateTime.DaysInMonth (DisplayDate.Year, DisplayDate.Month);
944 selector.RenderBeginTag (writer);
945 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + days, SelectMonthText, DayHeaderStyle.ForeColor, Enabled));
946 selector.RenderEndTag (writer);
950 DateTimeFormatInfo dti = DateInfo;
952 DayOfWeek dayOfWeek = (DayOfWeek) i;
953 dayName = dti.GetDayName (dayOfWeek);
956 if (UseAccessibleHeader) {
957 writer.AddAttribute (HtmlTextWriterAttribute.Abbr, dayName);
958 writer.AddAttribute (HtmlTextWriterAttribute.Scope, "col", false);
959 cell = new TableHeaderCell();
963 cell = new TableCell();
965 cell.HorizontalAlign = HorizontalAlign.Center;
966 cell.ApplyStyle (DayHeaderStyle);
968 cell.RenderBeginTag (writer);
970 switch (DayNameFormat) {
971 case DayNameFormat.FirstLetter:
972 dayName = dayName.Substring (0, 1);
974 case DayNameFormat.FirstTwoLetters:
975 dayName = dayName.Substring (0, 2);
978 case DayNameFormat.Shortest:
979 dayName = dti.GetShortestDayName (dayOfWeek);
982 case DayNameFormat.Full:
984 case DayNameFormat.Short:
986 dayName = dti.GetAbbreviatedDayName (dayOfWeek);
990 writer.Write (dayName);
991 cell.RenderEndTag (writer);
993 if (i >= daysInAWeek - 1) {
1003 writer.RenderEndTag ();
1006 void WriteDay (DateTime date, HtmlTextWriter writer)
1008 TableItemStyle style = new TableItemStyle ();
1009 TableCell cell = new TableCell ();
1011 CalendarDay day = new CalendarDay (date,
1012 IsWeekEnd (date.DayOfWeek),
1013 date == TodaysDate, SelectedDates.Contains (date),
1014 GetGlobalCalendar ().GetMonth (DisplayDate) != GetGlobalCalendar ().GetMonth (date),
1015 date.Day.ToString ());
1017 day.IsSelectable = SelectionMode != CalendarSelectionMode.None;
1018 cell.HorizontalAlign = HorizontalAlign.Center;
1019 cell.Width = Unit.Percentage (GetCellWidth ());
1021 LiteralControl lit = new LiteralControl (day.DayNumberText);
1022 cell.Controls.Add (lit);
1024 OnDayRender (cell, day);
1026 if (dayStyle != null && !dayStyle.IsEmpty) {
1027 style.CopyFrom (dayStyle);
1030 if (day.IsWeekend && weekendDayStyle != null && !weekendDayStyle.IsEmpty) {
1031 style.CopyFrom (weekendDayStyle);
1034 if (day.IsToday && todayDayStyle != null && !todayDayStyle.IsEmpty) {
1035 style.CopyFrom (todayDayStyle);
1038 if (day.IsOtherMonth && otherMonthDayStyle != null && !otherMonthDayStyle.IsEmpty) {
1039 style.CopyFrom (otherMonthDayStyle);
1042 if (day.IsSelected && Enabled) {
1043 style.BackColor = Color.Silver;
1044 style.ForeColor = Color.White;
1045 if (selectedDayStyle != null && !selectedDayStyle.IsEmpty) {
1046 style.CopyFrom (selectedDayStyle);
1050 cell.ApplyStyle (style);
1052 lit.Text = BuildLink (GetDaysFromZenith (date).ToString (), day.DayNumberText,
1053 cell.ForeColor, day.IsSelectable && Enabled);
1055 cell.RenderControl (writer);
1058 void WriteDays (HtmlTextWriter writer)
1060 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
1062 TableCell selectorCell = null;
1065 // Goes backwards until we find the date of that is begining of the week
1066 for (n = 0; n < daysInAWeek; n++) {
1067 if (date.DayOfWeek == DisplayFirstDayOfWeek)
1070 date = GetGlobalCalendar().AddDays (date, -1);
1072 /* if the start date is the first day of the week, we need to shift backward one more week */
1074 date = GetGlobalCalendar().AddDays (date, -1 * daysInAWeek);
1076 lastDate = GetGlobalCalendar().AddDays (date, 6 * daysInAWeek); // Always six weeks per months
1079 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1081 if (HasWeekSelectors (SelectionMode)) { // Week selector
1082 if (selectorCell == null) {
1083 selectorCell = new TableCell ();
1084 selectorCell.ApplyStyle (SelectorStyle);
1085 selectorCell.HorizontalAlign = HorizontalAlign.Center;
1086 selectorCell.Width = Unit.Percentage (GetCellWidth ());
1089 selectorCell.RenderBeginTag (writer);
1090 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + "07", SelectWeekText, selectorCell.ForeColor, Enabled));
1091 selectorCell.RenderEndTag (writer);
1094 for (int i = 0; i < daysInAWeek; i++) {
1095 WriteDay (date, writer);
1096 date = GetGlobalCalendar().AddDays (date, 1);
1099 writer.RenderEndTag ();
1100 if (date >= lastDate)
1105 string BuildLink (string arg, string text, Color foreColor, bool hasLink)
1107 StringBuilder str = new StringBuilder ();
1109 hasLink = (Page != null && hasLink == true) ? true : false;
1112 str.Append ("<a href=\"");
1114 str.Append (Page.ClientScript.GetPostBackClientHyperlink (this, arg, true));
1116 str.Append (Page.ClientScript.GetPostBackClientHyperlink (this, arg));
1121 if (!foreColor.IsEmpty) {
1124 if (ForeColor.IsEmpty)
1130 str.Append (" style=\"color:" + ColorTranslator.ToHtml (clr));
1133 str.Append ("</a>");
1138 return str.ToString ();
1141 int GetDaysFromZenith (DateTime date)
1143 TimeSpan span = date.Subtract (dateZenith);
1148 void WriteCaption (HtmlTextWriter writer)
1150 if (CaptionAlign != TableCaptionAlign.NotSet)
1151 writer.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString (Helpers.InvariantCulture));
1153 writer.RenderBeginTag (HtmlTextWriterTag.Caption);
1154 writer.Write (Caption);
1155 writer.RenderEndTag ();
1159 void WriteTitle (HtmlTextWriter writer)
1161 TableCell cellNextPrev = null;
1162 TableCell titleCell = new TableCell ();
1163 Table tableTitle = new Table ();
1165 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1167 titleCell.ColumnSpan = HasWeekSelectors (SelectionMode) ? 8 : 7;
1169 if (titleStyle != null && !titleStyle.IsEmpty && !titleStyle.BackColor.IsEmpty) {
1170 titleCell.BackColor = titleStyle.BackColor;
1172 titleCell.BackColor = Color.Silver;
1175 titleCell.RenderBeginTag (writer);
1178 tableTitle.Width = Unit.Percentage (100);
1179 if (titleStyle != null && !titleStyle.IsEmpty) {
1180 tableTitle.ApplyStyle (titleStyle);
1183 tableTitle.RenderBeginTag (writer);
1184 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1186 if (ShowNextPrevMonth) { // Previous Table Data
1187 cellNextPrev = new TableCell ();
1188 cellNextPrev.ApplyStyle (nextPrevStyle);
1189 cellNextPrev.Width = Unit.Percentage (15);
1191 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, - 1);
1192 date = GetGlobalCalendar ().AddDays (date, -date.Day + 1);
1193 cellNextPrev.RenderBeginTag (writer);
1194 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, false), cellNextPrev.ForeColor, Enabled));
1195 cellNextPrev.RenderEndTag (writer);
1198 // Current Month Table Data
1200 DateTimeFormatInfo dti = DateInfo;
1202 TableCell cellMonth = new TableCell ();
1203 cellMonth.Width = Unit.Percentage (70);
1204 cellMonth.HorizontalAlign = HorizontalAlign.Center;
1206 cellMonth.RenderBeginTag (writer);
1208 if (TitleFormat == TitleFormat.MonthYear)
1209 str = DisplayDate.ToString (dti.YearMonthPattern, dti);
1211 str = dti.GetMonthName (GetGlobalCalendar ().GetMonth (DisplayDate));
1214 cellMonth.RenderEndTag (writer);
1217 if (ShowNextPrevMonth) { // Next Table Data
1218 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, + 1);
1219 date = GetGlobalCalendar ().AddDays (date, -date.Day + 1);
1221 cellNextPrev.HorizontalAlign = HorizontalAlign.Right;
1222 cellNextPrev.RenderBeginTag (writer);
1223 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, true), cellNextPrev.ForeColor, Enabled));
1224 cellNextPrev.RenderEndTag (writer);
1227 writer.RenderEndTag ();
1228 tableTitle.RenderEndTag (writer);
1229 titleCell.RenderEndTag (writer);
1230 writer.RenderEndTag (); //tr
1235 string GetNextPrevFormatText (DateTime date, bool next)
1238 DateTimeFormatInfo dti = DateInfo;
1239 switch (NextPrevFormat) {
1240 case NextPrevFormat.FullMonth:
1241 text = dti.GetMonthName (GetGlobalCalendar ().GetMonth (date));
1243 case NextPrevFormat.ShortMonth:
1244 text = dti.GetAbbreviatedMonthName (GetGlobalCalendar ().GetMonth (date));
1246 case NextPrevFormat.CustomText:
1248 text = ((next) ? NextMonthText : PrevMonthText);
1255 bool IsWeekEnd (DayOfWeek day)
1257 return (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday);
1260 double GetCellWidth ()
1262 return HasWeekSelectors (SelectionMode) ? 100/8 : 100/7;
1265 System.Globalization.Calendar GetGlobalCalendar ()
1267 return DateTimeFormatInfo.CurrentInfo.Calendar;
1270 [WebSysDescription ("")]
1271 [WebCategory ("Action")]
1272 public event DayRenderEventHandler DayRender {
1274 Events.AddHandler (DayRenderEvent, value);
1277 Events.RemoveHandler (DayRenderEvent, value);
1281 [WebSysDescription ("")]
1282 [WebCategory ("Action")]
1283 public event EventHandler SelectionChanged {
1285 Events.AddHandler (SelectionChangedEvent, value);
1288 Events.RemoveHandler (SelectionChangedEvent, value);
1292 [WebSysDescription ("")]
1293 [WebCategory ("Action")]
1294 public event MonthChangedEventHandler VisibleMonthChanged {
1296 Events.AddHandler (VisibleMonthChangedEvent, value);
1299 Events.RemoveHandler (VisibleMonthChangedEvent, value);