days;
private string dataStartField;
private string dataEndField;
private string dataTextField;
private string dataValueField;
private string dataResourceField;
///
/// Event called when the user clicks an event in the calendar. It's only called when EventClickHandling is set to PostBack.
///
[Category("User actions")]
[Description("Event called when the user clicks an event in the calendar.")]
public event EventClickDelegate EventClick;
///
/// Event called when the user clicks a free space in the calendar. It's only called when FreeTimeClickHandling is set to PostBack.
///
[Category("User actions")]
[Description("Event called when the user clicks a free space in the calendar.")]
public event FreeClickDelegate FreeTimeClick;
///
/// Renders the component HTML code.
///
///
protected override void Render(HtmlTextWriter output)
{
loadEventsToDays();
renderMainTable(output);
}
private void renderMainTable(HtmlTextWriter output)
{
output.AddAttribute("id", ClientID);
output.AddStyleAttribute("width", Width + "px");
output.AddStyleAttribute("line-height", "1.2");
output.RenderBeginTag("div");
output.AddAttribute("cellspacing", "0");
output.AddAttribute("cellpadding", "0");
output.AddAttribute("border", "0");
output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(HourNameBackColor));
output.RenderBeginTag("table");
output.RenderBeginTag("tr");
output.AddStyleAttribute("width", (RowHeaderWidth) + "px");
output.RenderBeginTag("td");
renderCorner(output);
output.RenderEndTag(); // td
renderHeaderCols(output);
output.RenderEndTag(); // tr
// SOUTH
renderRows(output);
output.RenderEndTag(); // table
output.RenderEndTag(); // main
}
private void renderCells(HtmlTextWriter output, Day d)
{
int cellsToRender = cellCount;
for (int i = 0; i < cellsToRender; i++)
{
DateTime start = d.Start.AddMinutes(i*CellDuration);
int thisCellWidth = CellWidth;
if (i == 0)
{
thisCellWidth = CellWidth - 1;
}
string back = getCellColor(start, d.Value);
if (i == cellsToRender - 1)
{
output.AddStyleAttribute("border-right", "1px solid " + ColorTranslator.ToHtml(BorderColor));
}
output.AddStyleAttribute("border-bottom", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("width", thisCellWidth + "px");
output.AddStyleAttribute("background-color", back);
output.AddStyleAttribute("cursor", "pointer");
if (FreeTimeClickHandling == UserActionHandling.PostBack)
{
output.AddAttribute("onclick", "javascript:" + Page.ClientScript.GetPostBackEventReference(this, "TIME:" + start.ToString("s") + d.Value));
}
else
{
output.AddAttribute("onclick", "javascript:" + String.Format(FreeTimeClickJavaScript, start.ToString("s"), d.Value));
}
output.AddAttribute("onmouseover", "this.style.backgroundColor='" + ColorTranslator.ToHtml(HoverColor) + "';");
output.AddAttribute("onmouseout", "this.style.backgroundColor='" + back + "';");
output.RenderBeginTag("td");
output.Write("");
output.RenderEndTag();
}
}
private void renderEvents(Day d, HtmlTextWriter output)
{
if (d.events.Count == 0)
{
output.Write("");
}
else
{
output.AddStyleAttribute("position", "relative");
output.AddStyleAttribute("height", (d.MaxColumns() * EventHeight - 1) + "px"); //
output.AddStyleAttribute("overflow", "none");
output.AddAttribute("unselectable", "on");
output.RenderBeginTag("div");
foreach (Event ep in d.events)
{
renderEvent(d, ep, output);
}
// div relative
output.RenderEndTag();
}
}
private void renderEvent(Day d, Event p, HtmlTextWriter output)
{
int max = cellCount*CellWidth;
int left = (int)Math.Floor((p.BoxStart - d.Start).TotalMinutes * CellWidth / CellDuration);
int top = p.Column.Number * EventHeight - 1;
int width = (int)Math.Floor((p.BoxEnd - p.BoxStart).TotalMinutes * CellWidth / CellDuration) - 2;
int height = EventHeight - 1;
int startDelta = (int)Math.Floor((p.Start - p.BoxStart).TotalMinutes * CellWidth / CellDuration - 1);
int realWidth = (int)Math.Floor((p.End - p.Start).TotalMinutes * CellWidth / CellDuration);
realWidth = realWidth == 0 ? 1 : realWidth;
// adjustments
if (left > max) // don't render
{
return;
}
if (left + width > max - 2)
{
width = max - left - 2;
}
if (left < 0)
{
width += left;
left = 0;
}
width = Math.Max(width, 2);
output.AddAttribute("unselectable", "on");
output.AddStyleAttribute("position", "absolute");
output.AddStyleAttribute("left", left + "px");
output.AddStyleAttribute("top", top + "px");
output.AddStyleAttribute("width", width + "px");
output.AddStyleAttribute("height", height + "px");
output.AddStyleAttribute("border", "1px solid " + ColorTranslator.ToHtml(EventBorderColor));
output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(EventBackColor));
output.AddStyleAttribute("white-space", "nowrap");
output.AddStyleAttribute("overflow", "hidden");
output.AddStyleAttribute("font-family", EventFontFamily);
output.AddStyleAttribute("font-size", EventFontSize);
output.AddStyleAttribute("cursor", "pointer");
if (EventClickHandling == UserActionHandling.PostBack)
{
output.AddAttribute("onclick", "javascript:event.cancelBubble=true;" + Page.ClientScript.GetPostBackEventReference(this, "PK:" + p.PK));
}
else
{
output.AddAttribute("onclick", "javascript:event.cancelBubble=true;" + String.Format(EventClickJavaScript, p.PK));
}
output.RenderBeginTag("div");
if (DurationBarVisible)
{
output.Write("");
output.Write("");
}
output.AddStyleAttribute("display", "block");
output.AddStyleAttribute("padding-left", "1px");
output.AddAttribute("unselectable", "on");
output.RenderBeginTag("div");
output.Write(p.Name);
output.RenderEndTag();
output.RenderEndTag();
}
private void renderRows(HtmlTextWriter output)
{
foreach (Day d in days)
{
output.RenderBeginTag("tr");
renderRowHeader(output, d);
renderRowCells(output, d);
output.RenderEndTag();
}
}
private void renderRowCells(HtmlTextWriter output, Day d)
{
// render all events in the first cell
output.AddStyleAttribute("width", "1px");
output.AddStyleAttribute("border-bottom", "1px solid black");
output.AddStyleAttribute("background-color", getCellColor(d.Start, d.Value));
output.AddAttribute("valign", "top");
output.AddAttribute("unselectable", "on");
output.RenderBeginTag("td");
renderEvents(d, output);
// td
output.RenderEndTag();
renderCells(output, d);
}
private void renderRowHeader(HtmlTextWriter output, Day d)
{
int height = (d.MaxColumns() * EventHeight - 1);
output.AddStyleAttribute("width", (RowHeaderWidth - 1) + "px");
output.AddStyleAttribute("border-right", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("border-left", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("border-bottom", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(HourNameBackColor));
output.AddStyleAttribute("font-family", HeaderFontFamily);
output.AddStyleAttribute("font-size", HeaderFontSize);
output.AddStyleAttribute("color", ColorTranslator.ToHtml(HeaderFontColor));
output.AddStyleAttribute("cursor", "default");
output.AddAttribute("unselectable", "on");
output.AddAttribute("resource", d.Value);
output.RenderBeginTag("td");
output.Write("");
output.Write(d.Name);
output.Write("
");
output.RenderEndTag();
}
private void renderCorner(HtmlTextWriter output)
{
output.AddStyleAttribute("width", (RowHeaderWidth - 1) + "px");
output.AddStyleAttribute("height", (HeaderHeight - 1) + "px");
output.AddStyleAttribute("border-right", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("border-top", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("border-left", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("border-bottom", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(HourNameBackColor));
output.AddStyleAttribute("cursor", "default");
output.AddAttribute("unselectable", "on");
output.RenderBeginTag("div");
output.RenderEndTag(); // td
}
internal void renderHeaderCols(HtmlTextWriter output)
{
for (int i = 0; i < cellCount; i++)
{
DateTime from = StartDate.AddMinutes(CellDuration * i);
//DateTime to = from.AddMinutes(CellDuration);
string text;
if (CellDuration < 60) // smaller than hour, use minutes
{
text = String.Format("{0:00}", from.Minute);
}
else if (CellDuration < 1440)// smaller than day, use hours
{
text = TimeFormatter.GetHour(from, TimeFormat, "{0} {1}");
}
else // use days
{
text = from.Day.ToString();
}
if (i == 0)
{
output.AddAttribute("colspan", "2");
}
if (i == cellCount - 1)
{
output.AddStyleAttribute("border-right", "1px solid " + ColorTranslator.ToHtml(BorderColor));
}
output.AddStyleAttribute("border-top", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("border-bottom", "1px solid " + ColorTranslator.ToHtml(BorderColor));
output.AddStyleAttribute("width", (CellWidth) + "px");
output.AddStyleAttribute("height", (HeaderHeight - 1) + "px");
output.AddStyleAttribute("overflow", "hidden");
output.AddStyleAttribute("text-align", "center");
output.AddStyleAttribute("background-color", ColorTranslator.ToHtml(HourNameBackColor));
output.AddStyleAttribute("font-family", HourFontFamily);
output.AddStyleAttribute("font-size", HourFontSize);
output.AddAttribute("unselectable", "on");
output.AddStyleAttribute("-khtml-user-select", "none");
output.AddStyleAttribute("-moz-user-select", "none");
output.AddStyleAttribute("cursor", "default");
output.RenderBeginTag("td");
output.Write("");
output.Write(text);
output.Write("
");
output.RenderEndTag();
}
}
private void loadEventsToDays()
{
days = new List();
ArrayList items = (ArrayList)ViewState["Items"];
if (Resources == null)
{
return;
}
foreach (Resource resource in Resources)
{
Day d = new Day(StartDate, EndDate.AddDays(1), resource.Name, resource.Value, CellDuration);
days.Add(d);
}
foreach (Day d in days)
{
d.Load(items);
}
}
#region Properties
///
/// Gets or sets the first day to be shown. Default is DateTime.Today.
///
[Description("The first day to be shown. Default is DateTime.Today.")]
[Category("Behavior")]
public DateTime StartDate
{
get
{
if (ViewState["StartDate"] == null)
{
return DateTime.Today;
}
return (DateTime)ViewState["StartDate"];
}
set
{
ViewState["StartDate"] = new DateTime(value.Year, value.Month, value.Day);
}
}
///
/// Gets the last day to be shown.
///
[Browsable(false)]
public DateTime EndDate
{
get
{
return StartDate.AddDays(Days - 1);
}
}
///
/// Gets or sets the number of days to be displayed. Default is 1.
///
[Description("The number of days to be displayed on the calendar. Default value is 1.")]
[Category("Behavior")]
[DefaultValue(1)]
public int Days
{
get
{
if (ViewState["Days"] == null)
return 1;
return (int)ViewState["Days"];
}
set
{
int daysCount = value;
if (daysCount < 1)
daysCount = 1;
ViewState["Days"] = daysCount;
}
}
///
/// Cell size in minutes.
///
[Description("Cell width in pixels.")]
[Category("Layout")]
[DefaultValue(20)]
public virtual int CellWidth
{
get
{
if (ViewState["CellWidth"] == null)
return 20;
return (int)ViewState["CellWidth"];
}
set
{
ViewState["CellWidth"] = value;
}
}
///
/// Number of cells per hour horizontally.
///
[Description("Cell length in minutes.")]
[Category("Layout")]
[DefaultValue(60)]
public int CellDuration
{
get
{
if (ViewState["CellDuration"] == null)
return 60;
return (int)ViewState["CellDuration"];
}
set
{
ViewState["CellDuration"] = value;
}
}
///
/// Collection of rows (resources).
///
[Category("Behavior")]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Description("Collection of rows that will be used when ViewType property is set to ViewTypeEnum.Resources.")]
public ResourceCollection Resources
{
get
{
if (ViewState["Resources"] == null)
{
ResourceCollection rc = new ResourceCollection();
rc.designMode = DesignMode;
ViewState["Resources"] = rc;
}
return (ResourceCollection)ViewState["Resources"];
}
}
///
/// Gets or sets the name of the column that contains the event starting date and time (must be convertible to DateTime).
///
[Description("The name of the column that contains the event starting date and time (must be convertible to DateTime).")]
[Category("Data")]
public string DataStartField
{
get
{
return dataStartField;
}
set
{
dataStartField = value;
if (Initialized)
{
OnDataPropertyChanged();
}
}
}
///
/// Gets or sets the name of the column that contains the event ending date and time (must be convertible to DateTime).
///
[Description("The name of the column that contains the event ending date and time (must be convertible to DateTime).")]
[Category("Data")]
public string DataEndField
{
get
{
return dataEndField;
}
set
{
dataEndField = value;
if (Initialized)
{
OnDataPropertyChanged();
}
}
}
///
/// Gets or sets the name of the column that contains the name of an event.
///
[Category("Data")]
[Description("The name of the column that contains the name of an event.")]
public string DataTextField
{
get
{
return dataTextField;
}
set
{
dataTextField = value;
if (Initialized)
{
OnDataPropertyChanged();
}
}
}
///
/// Gets or sets the name of the column that contains the primary key. The primary key will be used for rendering the custom JavaScript actions.
///
[Category("Data")]
[Description("The name of the column that contains the primary key. The primary key will be used for rendering the custom JavaScript actions.")]
public string DataValueField
{
get
{
return dataValueField;
}
set
{
dataValueField = value;
if (Initialized)
{
OnDataPropertyChanged();
}
}
}
///
/// Gets or sets the name of the column that contains the primary key. The primary key will be used for rendering the custom JavaScript actions.
///
[Category("Data")]
[Description("The name of the column that contains the column id.")]
public string DataResourceField
{
get
{
return dataResourceField;
}
set
{
dataResourceField = value;
if (Initialized)
{
OnDataPropertyChanged();
}
}
}
///
/// Color of the hour names background.
///
[Category("Appearance")]
[TypeConverter(typeof(WebColorConverter))]
[Description("Color of the hour names background.")]
public Color HourNameBackColor
{
get
{
if (ViewState["HourNameBackColor"] == null)
return ColorTranslator.FromHtml("#ECE9D8");
return (Color)ViewState["HourNameBackColor"];
}
set
{
ViewState["HourNameBackColor"] = value;
}
}
///
/// Color of the horizontal border that separates our names.
///
[Category("Appearance")]
[TypeConverter(typeof(WebColorConverter))]
[Description("Color of the horizontal border that separates our names.")]
public Color HourNameBorderColor
{
get
{
if (ViewState["HourNameBorderColor"] == null)
return ColorTranslator.FromHtml("#ACA899");
return (Color)ViewState["HourNameBorderColor"];
}
set
{
ViewState["HourNameBorderColor"] = value;
}
}
///
/// Color of an event border.
///
[Category("Appearance")]
[TypeConverter(typeof(WebColorConverter))]
[Description("Color of an event border.")]
public Color EventBorderColor
{
get
{
if (ViewState["EventBorderColor"] == null)
return ColorTranslator.FromHtml("#000000");
return (Color)ViewState["EventBorderColor"];
}
set
{
ViewState["EventBorderColor"] = value;
}
}
///
/// Color of an event background.
///
[Category("Appearance")]
[TypeConverter(typeof(WebColorConverter))]
[Description("Color of an event background.")]
public Color EventBackColor
{
get
{
if (ViewState["EventBackColor"] == null)
return ColorTranslator.FromHtml("#FFFFFF");
return (Color)ViewState["EventBackColor"];
}
set
{
ViewState["EventBackColor"] = value;
}
}
///
///Gets or sets the background color of the Web server control.
///
///
///
///A that represents the background color of the control. The default is , which indicates that this property is not set.
///
///
public override Color BackColor
{
get
{
if (ViewState["BackColor"] == null)
return ColorTranslator.FromHtml("#FFFFD5");
return (Color)ViewState["BackColor"];
}
set
{
ViewState["BackColor"] = value;
}
}
///
/// Gets or sets the start of the business day (in hours).
///
[Description("Start of the business day (hour from 0 to 23).")]
[Category("Behavior")]
[DefaultValue(9)]
public int BusinessBeginsHour
{
get
{
if (ViewState["BusinessBeginsHour"] == null)
return 9;
return (int)ViewState["BusinessBeginsHour"];
}
set
{
if (value < 0)
ViewState["BusinessBeginsHour"] = 0;
else if (value > 23)
ViewState["BusinessBeginsHour"] = 23;
else
ViewState["BusinessBeginsHour"] = value;
}
}
///
/// Gets or sets the end of the business day (hours).
///
[Description("End of the business day (hour from 1 to 24).")]
[Category("Behavior")]
[DefaultValue(18)]
public int BusinessEndsHour
{
get
{
if (ViewState["BusinessEndsHour"] == null)
return 18;
return (int)ViewState["BusinessEndsHour"];
}
set
{
if (value < BusinessBeginsHour)
ViewState["BusinessEndsHour"] = BusinessBeginsHour + 1;
else if (value > 24)
ViewState["BusinessEndsHour"] = 24;
else
ViewState["BusinessEndsHour"] = value;
}
}
///
/// Font family of the row header, e.g. "Tahoma".
///
[Description("Font family of the event text, e.g. \"Tahoma\".")]
[Category("Appearance")]
[DefaultValue("Tahoma")]
public string HeaderFontFamily
{
get
{
if (ViewState["HeaderFontFamily"] == null)
return "Tahoma";
return (string)ViewState["HeaderFontFamily"];
}
set
{
ViewState["HeaderFontFamily"] = value;
}
}
///
/// Color of the column header font.
///
[Description("Color of the column header font.")]
[Category("Appearance")]
public Color HeaderFontColor
{
get
{
if (ViewState["HeaderFontColor"] == null)
return ColorTranslator.FromHtml("#000000");
return (Color)ViewState["HeaderFontColor"];
}
set
{
ViewState["HeaderFontColor"] = value;
}
}
///
/// Font size of the row header, e.g. "10pt".
///
[Description("Font size of the row header, e.g. \"10pt\".")]
[Category("Appearance")]
[DefaultValue("10pt")]
public string HeaderFontSize
{
get
{
if (ViewState["HeaderFontSize"] == null)
return "10pt";
return (string)ViewState["HeaderFontSize"];
}
set
{
ViewState["HeaderFontSize"] = value;
}
}
///
/// Font family of the time axis headers, e.g. "Tahoma".
///
[Category("Appearance")]
[Description("Font family of the hour names (horizontal axis), e.g. \"Tahoma\".")]
[DefaultValue("Tahoma")]
public string HourFontFamily
{
get
{
if (ViewState["HourFontFamily"] == null)
return "Tahoma";
return (string)ViewState["HourFontFamily"];
}
set
{
ViewState["HourFontFamily"] = value;
}
}
///
/// Font size of the time axis header e.g. "16pt".
///
[Description("Font size of the hour names (horizontal axis), e.g. \"10pt\".")]
[Category("Appearance")]
[DefaultValue("10pt")]
public string HourFontSize
{
get
{
if (ViewState["HourFontSize"] == null)
return "10pt";
return (string)ViewState["HourFontSize"];
}
set
{
ViewState["HourFontSize"] = value;
}
}
///
/// Font family of the event text, e.g. "Tahoma".
///
[Category("Appearance")]
[DefaultValue("Tahoma")]
[Description("Font family of the event text, e.g. \"Tahoma\".")]
public string EventFontFamily
{
get
{
if (ViewState["EventFontFamily"] == null)
return "Tahoma";
return (string)ViewState["EventFontFamily"];
}
set
{
ViewState["EventFontFamily"] = value;
}
}
///
/// Font size of the event text, e.g. "8pt".
///
[Category("Appearance")]
[DefaultValue("7pt")]
[Description("Font size of the event text, e.g. \"7pt\".")]
public string EventFontSize
{
get
{
if (ViewState["EventFontSize"] == null)
return "7pt";
return (string)ViewState["EventFontSize"];
}
set
{
ViewState["EventFontSize"] = value;
}
}
///
/// Height of the event cell in pixels.
///
[Category("Appearance")]
[DefaultValue("17")]
[Description("Height of the event cell in pixels.")]
public int EventHeight
{
get
{
if (ViewState["EventHeight"] == null)
return 17;
return (int)ViewState["EventHeight"];
}
set
{
ViewState["EventHeight"] = value;
}
}
///
/// Height of the header cells (with hour names) in pixels.
///
[Category("Appearance")]
[DefaultValue("17")]
[Description("Height of the header cells (with hour names) in pixels.")]
public int HeaderHeight
{
get
{
if (ViewState["HeaderHeight"] == null)
return 17;
return (int)ViewState["HeaderHeight"];
}
set
{
ViewState["HeaderHeight"] = value;
}
}
///
/// Background color of time cells outside of the busines hours.
///
[Category("Appearance")]
[DefaultValue("#FFF4BC")]
[TypeConverter(typeof(WebColorConverter))]
[Description("Background color of time cells outside of the busines hours.")]
public Color NonBusinessBackColor
{
get
{
if (ViewState["NonBusinessBackColor"] == null)
return ColorTranslator.FromHtml("#FFF4BC");
return (Color)ViewState["NonBusinessBackColor"];
}
set
{
ViewState["NonBusinessBackColor"] = value;
}
}
///
/// Color of the horizontal border that separates hour names.
///
[Category("Appearance")]
[TypeConverter(typeof(WebColorConverter))]
[Description("Color of the vertical border that separates hour names.")]
public Color HourBorderColor
{
get
{
if (ViewState["HourBorderColor"] == null)
return ColorTranslator.FromHtml("#EAD098");
return (Color)ViewState["HourBorderColor"];
}
set
{
ViewState["HourBorderColor"] = value;
}
}
///
///Gets or sets the border color of the Web control.
///
///
///
///A that represents the border color of the control. The default is , which indicates that this property is not set.
///
public override Color BorderColor
{
get
{
if (ViewState["BorderColor"] == null)
return ColorTranslator.FromHtml("#000000");
return (Color)ViewState["BorderColor"];
}
set
{
ViewState["BorderColor"] = value;
}
}
///
/// Color of the horizontal bar on the top of an event.
///
[Category("Appearance")]
[TypeConverter(typeof(WebColorConverter))]
[Description("Color of the horizontal bar on the top of an event.")]
public Color DurationBarColor
{
get
{
if (ViewState["DurationBarColor"] == null)
return ColorTranslator.FromHtml("blue");
return (Color)ViewState["DurationBarColor"];
}
set
{
ViewState["DurationBarColor"] = value;
}
}
///
/// Gets or sets the time-format for hour numbers (on the top).
///
[Category("Appearance")]
[Description("The time-format that will be used for the hour numbers.")]
[DefaultValue(TimeFormat.Clock12Hours)]
public TimeFormat TimeFormat
{
get
{
if (ViewState["TimeFormat"] == null)
return TimeFormat.Clock12Hours;
return (TimeFormat)ViewState["TimeFormat"];
}
set
{
ViewState["TimeFormat"] = value;
}
}
///
/// Width of the control (pixels and percentage values supported).
///
[Description("Width of the control (pixels and percentage values supported).")]
public new int Width
{
get
{
return RowHeaderWidth + cellCount * CellWidth;
}
}
///
/// Width of the row header (resource names) in pixels.
///
[Description("Width of the row header (resource names) in pixels.")]
[Category("Appearance")]
[DefaultValue(80)]
public int RowHeaderWidth
{
get
{
if (ViewState["RowHeaderWidth"] == null)
return 80;
return (int)ViewState["RowHeaderWidth"];
}
set
{
ViewState["RowHeaderWidth"] = value;
}
}
///
/// Whether the color bar on the left side of and event should be visible.
///
[Category("Appearance")]
[DefaultValue(true)]
[Description("Whether the duration bar on the top of and event should be visible.")]
public bool DurationBarVisible
{
get
{
if (ViewState["DurationBarVisible"] == null)
return true;
return (bool)ViewState["DurationBarVisible"];
}
set
{
ViewState["DurationBarVisible"] = value;
}
}
///
/// Handling of user action (clicking an event).
///
[Category("User actions")]
[Description("Whether clicking an event should do a postback or run a javascript action. By default, it calls the javascript code specified in EventClickJavaScript property.")]
[DefaultValue(UserActionHandling.JavaScript)]
public UserActionHandling EventClickHandling
{
get
{
if (ViewState["EventClickHandling"] == null)
return UserActionHandling.JavaScript;
return (UserActionHandling)ViewState["EventClickHandling"];
}
set
{
ViewState["EventClickHandling"] = value;
}
}
///
/// Gets or sets the Javascript code that is executed when the users clicks on an event. '{0}' will be replaced by the primary key of the event.
///
[Description("Javascript code that is executed when the users clicks on an event. '{0}' will be replaced by the primary key of the event.")]
[Category("User actions")]
[DefaultValue("alert('{0}');")]
public string EventClickJavaScript
{
get
{
if (ViewState["EventClickJavaScript"] == null)
return "alert('{0}');";
return (string)ViewState["EventClickJavaScript"];
}
set
{
ViewState["EventClickJavaScript"] = value;
}
}
///
/// Handling of user action (clicking a free-time slot).
///
[Category("User actions")]
[Description("Whether clicking a free-time slot should do a postback or run a javascript action. By default, it calls the javascript code specified in FreeTimeClickJavaScript property.")]
[DefaultValue(UserActionHandling.JavaScript)]
public UserActionHandling FreeTimeClickHandling
{
get
{
if (ViewState["FreeTimeClickHandling"] == null)
return UserActionHandling.JavaScript;
return (UserActionHandling)ViewState["FreeTimeClickHandling"];
}
set
{
ViewState["FreeTimeClickHandling"] = value;
}
}
///
/// Gets or sets the Javascript code that is executed when the users clicks on a free time slot. '{0}' will be replaced by the starting time of that slot (i.e. '9:00'.
///
[Description("Javascript code that is executed when the users clicks on a free time slot. '{0}' will be replaced by the starting time of that slot (i.e. '9:00'.")]
[Category("User actions")]
[DefaultValue("alert('{0}, {1}');")]
public string FreeTimeClickJavaScript
{
get
{
if (ViewState["FreeTimeClickJavaScript"] == null)
return "alert('{0}, {1}');";
return (string)ViewState["FreeTimeClickJavaScript"];
}
set
{
ViewState["FreeTimeClickJavaScript"] = value;
}
}
[Category("Appearance")]
[TypeConverter(typeof(WebColorConverter))]
public Color HoverColor
{
get
{
if (ViewState["HoverColor"] == null)
return ColorTranslator.FromHtml("#FFED95");
return (Color)ViewState["HoverColor"];
}
set
{
ViewState["HoverColor"] = value;
}
}
#endregion
#region Data binding
///
///Retrieves data from the associated data source.
///
///
protected override void PerformSelect()
{
if (!IsBoundUsingDataSourceID)
{
this.OnDataBinding(EventArgs.Empty);
}
GetData().Select(CreateDataSourceSelectArguments(), this.OnDataSourceViewSelectCallback);
RequiresDataBinding = false;
MarkAsDataBound();
OnDataBound(EventArgs.Empty);
}
private void OnDataSourceViewSelectCallback(IEnumerable retrievedData)
{
if (IsBoundUsingDataSourceID)
{
OnDataBinding(EventArgs.Empty);
}
PerformDataBinding(retrievedData);
}
///
///Binds data from the data source to the control.
///
///
///The list of data returned from a method call.
protected override void PerformDataBinding(IEnumerable retrievedData)
{
ViewState["Items"] = new ArrayList();
// don't load events in design mode
if (DesignMode)
{
return;
}
base.PerformDataBinding(retrievedData);
// Verify data exists.
if (retrievedData == null)
{
return;
}
if (String.IsNullOrEmpty(DataStartField))
{
throw new NullReferenceException("DataStartField property must be specified.");
}
if (String.IsNullOrEmpty(DataEndField))
{
throw new NullReferenceException("DataEndField property must be specified.");
}
if (String.IsNullOrEmpty(DataTextField))
{
throw new NullReferenceException("DataTextField property must be specified.");
}
if (String.IsNullOrEmpty(DataValueField))
{
throw new NullReferenceException("DataValueField property must be specified.");
}
if (String.IsNullOrEmpty(DataResourceField))
{
throw new NullReferenceException("DataResourceField property must be specified.");
}
foreach (object dataItem in retrievedData)
{
DateTime start;
DateTime end;
string strStart = DataBinder.GetPropertyValue(dataItem, DataStartField, null);
if (!DateTime.TryParse(strStart, out start))
{
throw new FormatException(String.Format("Unable to convert '{0}' (from DataStartField column) to DateTime.", strStart));
}
string strEnd = DataBinder.GetPropertyValue(dataItem, DataEndField, null);
if (!DateTime.TryParse(strEnd, out end))
{
throw new FormatException(String.Format("Unable to convert '{0}' (from DataEndField column) to DateTime.", strEnd));
}
string name = DataBinder.GetPropertyValue(dataItem, DataTextField, null);
string val = DataBinder.GetPropertyValue(dataItem, DataValueField, null);
string resourceId = Convert.ToString(DataBinder.GetPropertyValue(dataItem, DataResourceField, null));
((ArrayList)ViewState["Items"]).Add(new Event(val, start, end, name, resourceId));
}
((ArrayList)ViewState["Items"]).Sort(new EventComparer());
}
#endregion
private int cellCount
{
get
{
return Days * 24 * 60 / CellDuration;
}
}
internal string getCellColor(DateTime from, string resourceId)
{
bool isBusiness = isBusinessCell(from);
if (isBusiness)
{
return ColorTranslator.ToHtml(BackColor);
}
else
{
return ColorTranslator.ToHtml(NonBusinessBackColor);
}
}
private bool isBusinessCell(DateTime from)
{
if (from.DayOfWeek == DayOfWeek.Saturday || from.DayOfWeek == DayOfWeek.Sunday)
{
return false;
}
else
{
if (CellDuration < 720) // use hours
{
if (from.Hour < BusinessBeginsHour || from.Hour >= BusinessEndsHour)
{
return false;
}
else
{
return true;
}
}
else // use days
{
return true;
}
}
}
///
///When implemented by a class, enables a server control to process an event raised when a form is posted to the server.
///
///
///A that represents an optional event argument to be passed to the event handler.
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument.StartsWith("PK:"))
{
string pk = eventArgument.Substring(3, eventArgument.Length - 3);
if (EventClick != null)
EventClick(this, new EventClickEventArgs(pk));
}
else if (eventArgument.StartsWith("TIME:"))
{
DateTime time = Convert.ToDateTime(eventArgument.Substring(5, 19));
string resource = eventArgument.Substring(24);
if (FreeTimeClick != null)
FreeTimeClick(this, new FreeClickEventArgs(time, resource));
}
else
{
throw new ArgumentException("Bad argument passed from postback event.");
}
}
}
}