コード例 #1
0
        /// <summary>
        /// Eventhandler that handles form validation.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void submit_Click(object sender, EventArgs e)
        {
            if (FieldControls.Where(o => !o.IsValid()).Count() == 0)
            {
                SendSubmittedFormDataEmail();
            }
            else
            {
                var invalidControl = FieldControls.Where(o => !o.IsValid()).FirstOrDefault();

                if (invalidControl is FieldControl)
                {
                    Page.SetFocus(((FieldControl)invalidControl).ClientID);
                }
                else if (invalidControl is FormCaptcha)
                {
                    Page.SetFocus(((FormCaptcha)invalidControl).ClientID);
                }
                else if (invalidControl is FormCheckboxes)
                {
                    Page.SetFocus(((FormCheckboxes)invalidControl).ClientID);
                }
            }
        }
        private DbBaseFieldControl ItemRow(DbField fld, List <DbBaseFieldControl> subcontrols = null)
        {
            var fldRow = new TableRow();

            if (fld.Hidden)
            {
                fldRow.Style.Add("display", "none");
            }
            var titleCell = new TableCell {
                CssClass      = "ms-formlabel",
                VerticalAlign = VerticalAlign.Top,
                Width         = Unit.Pixel(113)
            };

            var displayName = fld.DisplayName;
            var lookup      = fld as DbFieldLookup;

            if (lookup != null)
            {
                if (lookup.ListSource == (int)LookupSourceType.SpList)
                {
                    var list = SPContext.Current.SPList(lookup.ListId.ToGuid());
                    if (list != null)
                    {
                        if (lookup.LookupFields.Count() > 1)
                        {
                            displayName = string.Format("{0} : {1}", fld.DisplayName,
                                                        list.FieldTitle(lookup.LookupFields[0].ToGuid()));
                        }
                    }
                }
            }

            var fldLabel = new Label {
                ID   = fld.InternalName + "_label",
                Text = displayName
            };

            titleCell.Controls.Add(fldLabel);

            var bodyCell = new TableCell {
                CssClass      = "ms-formbody",
                VerticalAlign = VerticalAlign.Top,
                Width         = Unit.Pixel(350)
            };

            bodyCell.Attributes.Add("fieldname", fld.InternalName);
            var baseCtrl = fld.BaseControl;

            baseCtrl.ID          = fld.InternalNameOriginal.ToNormalize();
            baseCtrl.ControlMode = ControlMode;
            if (subcontrols != null)
            {
                baseCtrl.Subcontrols.AddRange(subcontrols);
            }
            bodyCell.Controls.Add(baseCtrl);

            fldRow.Cells.Add(titleCell);
            fldRow.Cells.Add(bodyCell);

            FormTable.Rows.AddAt(0, fldRow);

            FieldControls.Add(new Tuple <Label, DbBaseFieldControl, DbField>(fldLabel, baseCtrl, fld));
            return(baseCtrl);
        }
        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
                });
            }
        }