protected override void OnInit(EventArgs e)
        {
            if (ControlMode == SPControlMode.Invalid)
            {
                ControlMode = SPControlMode.Display;
            }
            base.OnInit(e);

            try
            {
                if (!this.HasRights)
                {
                    throw new Exception("You are not allowed to view this page!");
                }

                // History LINK
                if (ControlMode == SPControlMode.Display && !string.IsNullOrEmpty(this.HistoryLinkUrl))
                {
                    var historyRow = new TableRow();
                    var titleCell  = new TableCell {
                        CssClass = "ms-formlabel", VerticalAlign = VerticalAlign.Top, Width = Unit.Pixel(113), Text = "History"
                    };
                    var bodyCell = new TableCell {
                        CssClass = "ms-formbody", VerticalAlign = VerticalAlign.Top, Width = Unit.Pixel(350)
                    };
                    var linkCtrl = new HyperLink();
                    linkCtrl.Text        = "view status history...";
                    linkCtrl.NavigateUrl = this.HistoryLinkUrl + "?RosterId=" + this.ItemId;
                    linkCtrl.Target      = "_blank";
                    bodyCell.Controls.Add(linkCtrl);

                    historyRow.Cells.Add(titleCell);
                    historyRow.Cells.Add(bodyCell);
                    FormTable.Rows.AddAt(0, historyRow);
                }

                foreach (var fld in ListMetadataFields.OrderByDescending(item => item.Item1).Select(fieldItem => fieldItem.Item2.GetDbField()))
                {
                    var subcontrols = new List <DbBaseFieldControl>();
                    fld.SubFields().ForEach(item => {
                        var control = ItemRow(item);
                        subcontrols.Add(control);
                    });
                    ItemRow(fld, subcontrols);
                }
            }
            catch (Exception ex)
            {
                ErrorHolder.Controls.Add(new Label {
                    Text = ex.Message, ForeColor = System.Drawing.Color.Red
                });
                //ErrorHolder.Controls.Add(new Label { Text = ex.StackTrace, ForeColor = System.Drawing.Color.Red });
            }
        }
        private void AddSystemInfoRow(string prefix, string fld1, string fld2)
        {
            Tuple <int, ListMetadataField> userField = null;

            if (this.ItemId.HasValue && Item.RosterEventDictionary.ContainsKey(fld1) && Item.RosterEventDictionary.ContainsKey(fld2) &&
                Item.RosterEventDictionary[fld1] != null && Item.RosterEventDictionary[fld2] != null &&
                (userField = ListMetadataFields.FirstOrDefault(fld => fld.Item2.InternalName.Equals(fld2))) != null)
            {
                var createdRow  = new TableRow();
                var createdCell = new TableCell {
                    Text = String.Format("{0} at {1} by {2}", prefix, ((DateTime)Item.RosterEventDictionary[fld1]).ToString("dd/MM/yyyy HH:mm"),
                                         (userField.Item2.GetDbField() as DbFieldUser).EnsureUser(Item.RosterEventDictionary[fld2].ToInt()).DisplayText),
                    CssClass = "ms-descriptiontext"
                };
                createdRow.Cells.Add(createdCell);
                tblCreatedModified.Rows.Add(createdRow);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var rosterEvent = (ControlMode == SPControlMode.New) ? _dataService.CreateRosterEvent(ListId, (int)EventType): Item;
                if (rosterEvent == null)
                {
                    throw new Exception("Roster event is empty");
                }
                foreach (var field in ListMetadataFields.Select(item => item.Item2))
                {
                    var uiControls = FieldControls.FirstOrDefault(item => item.Item2.ID == field.InternalName.ToString());
                    if (uiControls == null)
                    {
                        continue;
                    }
                    if (uiControls.Item3.ReadOnly)
                    {
                        if (Request.QueryString[field.InternalName] != null)
                        {
                            rosterEvent.RosterEventDictionary[field.InternalName] = Request.QueryString[field.InternalName];
                        }
                    }
                    else
                    {
                        rosterEvent.RosterEventDictionary[field.InternalName] = uiControls.Item2.Value;
                    }
                    if (uiControls.Item3.Required && string.IsNullOrWhiteSpace(uiControls.Item2.Value.ToSafeString()))
                    {
                        ErrorHolder.Controls.Add(new Label
                        {
                            Text = @"Please fill out all required fields", ForeColor = System.Drawing.Color.Red
                        });
                        return;
                    }
                }

                // set ContentType
                if (rosterEvent.RosterEventDictionary.ContainsKey(FieldNames.CONTENT_TYPE_ID))
                {
                    if (rosterEvent.RosterEventDictionary[FieldNames.CONTENT_TYPE_ID] == null)
                    {
                        rosterEvent.RosterEventDictionary[FieldNames.CONTENT_TYPE_ID] = ContentTypeId;
                    }
                }
                else
                {
                    rosterEvent.RosterEventDictionary.Add(FieldNames.CONTENT_TYPE_ID, ContentTypeId);
                }

                // set StartDate and EndDate for AllDayEvent
                if (rosterEvent.RosterEventDictionary.ContainsKey(FieldNames.ALL_DAY_EVENT) &&
                    rosterEvent.RosterEventDictionary.ContainsKey(FieldNames.START_DATE) &&
                    rosterEvent.RosterEventDictionary.ContainsKey(FieldNames.END_DATE))
                {
                    var allDayEventObj = rosterEvent.RosterEventDictionary[FieldNames.ALL_DAY_EVENT];
                    if (allDayEventObj != null && allDayEventObj.ToBoolean())
                    {
                        var startDt = rosterEvent.RosterEventDictionary[FieldNames.START_DATE];
                        var endDt   = rosterEvent.RosterEventDictionary[FieldNames.END_DATE];
                        if (startDt != null && endDt != null)
                        {
                            rosterEvent.RosterEventDictionary[FieldNames.START_DATE] = ((DateTime)startDt).Date;
                            rosterEvent.RosterEventDictionary[FieldNames.END_DATE]   = ((DateTime)endDt).Date.AddMinutes(1439);
                        }
                    }
                }

                // check by Master Roster - if it is a Template - NO new rosters
                var masterRosterId = (rosterEvent.RosterEventDictionary.ContainsKey(FieldNames.MASTER_ROSTER_ID)) ?
                                     rosterEvent.RosterEventDictionary[FieldNames.MASTER_ROSTER_ID] : null;
                if (ControlMode == SPControlMode.New && this.ListId == TableIDs.PLANNED_ROSTERS &&
                    masterRosterId != null && _dataService.IsTemplate(masterRosterId.ToInt()))
                {
                    throw new Exception("It is not possible to modify Roster Template!"); // do not allow ADD to Template
                }

                _dataService.SaveRosterEvent(rosterEvent, ListId);
                Utils.GoBackOnSuccess(this.Page, this.Context);
            }
            catch (Exception ex)
            {
                ErrorHolder.Controls.Add(new Label
                {
                    Text = ex.ToReadbleSrting("Saving Error"), ForeColor = System.Drawing.Color.Red
                });
            }
        }