示例#1
0
        /// <summary>
        /// Adds the control.
        /// </summary>
        /// <param name="controls">The controls.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public Control AddControl(ControlCollection controls, AttributeControlOptions options)
        {
            var attributeControl = FieldType.Field.EditControl(QualifierValues, options.SetId ? options.AttributeControlId : string.Empty);

            if (attributeControl == null)
            {
                return(null);
            }

            if (options.SetId)
            {
                attributeControl.ClientIDMode = ClientIDMode.AutoID;
            }

            // If the control is a RockControl
            var rockControl        = attributeControl as IRockControl;
            var controlHasRequired = attributeControl as IHasRequired;

            if (rockControl != null)
            {
                rockControl.Label           = options.LabelText;
                rockControl.Help            = options.HelpText;
                rockControl.Warning         = options.WarningText;
                rockControl.Required        = options.Required ?? IsRequired;
                rockControl.ValidationGroup = options.ValidationGroup;

                controls.Add(attributeControl);
            }
            else
            {
                if (controlHasRequired != null)
                {
                    controlHasRequired.Required        = options.Required ?? IsRequired;
                    controlHasRequired.ValidationGroup = options.ValidationGroup;
                }

                bool renderLabel   = !string.IsNullOrEmpty(options.LabelText);
                bool renderHelp    = !string.IsNullOrWhiteSpace(options.HelpText);
                bool renderWarning = !string.IsNullOrWhiteSpace(options.WarningText);

                if (renderLabel || renderHelp || renderWarning)
                {
                    var div = new DynamicControlsHtmlGenericControl("div")
                    {
                        ID = $"_formgroup_div_{Id}"
                    };
                    controls.Add(div);

                    div.Controls.Clear();
                    div.AddCssClass("form-group");
                    if (IsRequired)
                    {
                        div.AddCssClass("required");
                    }

                    div.ClientIDMode = ClientIDMode.AutoID;

                    if (renderLabel)
                    {
                        var label = new Label
                        {
                            ID                  = $"_label_{Id}",
                            ClientIDMode        = ClientIDMode.AutoID,
                            Text                = options.LabelText,
                            CssClass            = "control-label",
                            AssociatedControlID = attributeControl.ID
                        };
                        div.Controls.Add(label);
                    }

                    if (renderHelp)
                    {
                        var helpBlock = new HelpBlock
                        {
                            ID           = $"_helpBlock_{Id}",
                            ClientIDMode = ClientIDMode.AutoID,
                            Text         = options.HelpText
                        };
                        div.Controls.Add(helpBlock);
                    }

                    if (renderWarning)
                    {
                        var warningBlock = new WarningBlock
                        {
                            ID           = $"_warningBlock_{Id}",
                            ClientIDMode = ClientIDMode.AutoID,
                            Text         = options.WarningText
                        };
                        div.Controls.Add(warningBlock);
                    }

                    div.Controls.Add(attributeControl);
                }
                else
                {
                    controls.Add(attributeControl);
                }
            }

            if (options.SetValue)
            {
                FieldType.Field.SetEditValue(attributeControl, QualifierValues, options.Value);
            }

            return(attributeControl);
        }
示例#2
0
        /// <summary>
        /// Adds the control.
        /// </summary>
        /// <param name="controls">The controls.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public Control AddControl(ControlCollection controls, AttributeControlOptions options)
        {
            options.LabelText          = options.LabelText ?? Name;
            options.HelpText           = options.HelpText ?? Description;
            options.AttributeControlId = options.AttributeControlId ?? $"attribute_field_{Id}";

            EntityTypeCache entityType = EntityTypeId.HasValue ? EntityTypeCache.Get(this.EntityTypeId.Value) : null;

            bool showPrePostHtml = (entityType?.AttributesSupportPrePostHtml ?? false) && (options?.ShowPrePostHtml ?? true);

            var attributeControl = FieldType.Field.EditControl(QualifierValues, options.SetId ? options.AttributeControlId : string.Empty);

            if (attributeControl == null)
            {
                return(null);
            }

            if (options.SetId)
            {
                attributeControl.ClientIDMode = ClientIDMode.AutoID;
            }

            // If the control is a RockControl
            var rockControl        = attributeControl as IRockControl;
            var controlHasRequired = attributeControl as IHasRequired;

            if (showPrePostHtml)
            {
                if (this.PreHtml.IsNotNullOrWhiteSpace())
                {
                    controls.Add(new Literal {
                        Text = this.PreHtml
                    });
                }
            }

            if (rockControl != null)
            {
                rockControl.Label           = options.LabelText;
                rockControl.Help            = options.HelpText;
                rockControl.Warning         = options.WarningText;
                rockControl.Required        = options.Required ?? IsRequired;
                rockControl.ValidationGroup = options.ValidationGroup;

                controls.Add(attributeControl);
            }
            else
            {
                if (controlHasRequired != null)
                {
                    controlHasRequired.Required        = options.Required ?? IsRequired;
                    controlHasRequired.ValidationGroup = options.ValidationGroup;
                }

                bool renderLabel   = !string.IsNullOrEmpty(options.LabelText);
                bool renderHelp    = !string.IsNullOrWhiteSpace(options.HelpText);
                bool renderWarning = !string.IsNullOrWhiteSpace(options.WarningText);

                if (renderLabel || renderHelp || renderWarning)
                {
                    var div = new DynamicControlsHtmlGenericControl("div")
                    {
                        ID = $"_formgroup_div_{Id}"
                    };
                    controls.Add(div);

                    div.Controls.Clear();
                    div.AddCssClass("form-group");
                    if (IsRequired)
                    {
                        div.AddCssClass("required");
                    }

                    div.ClientIDMode = ClientIDMode.AutoID;

                    if (renderLabel)
                    {
                        var label = new Label
                        {
                            ID                  = $"_label_{Id}",
                            ClientIDMode        = ClientIDMode.AutoID,
                            Text                = options.LabelText,
                            CssClass            = "control-label",
                            AssociatedControlID = attributeControl.ID
                        };
                        div.Controls.Add(label);
                    }

                    if (renderHelp)
                    {
                        var helpBlock = new HelpBlock
                        {
                            ID           = $"_helpBlock_{Id}",
                            ClientIDMode = ClientIDMode.AutoID,
                            Text         = options.HelpText
                        };
                        div.Controls.Add(helpBlock);
                    }

                    if (renderWarning)
                    {
                        var warningBlock = new WarningBlock
                        {
                            ID           = $"_warningBlock_{Id}",
                            ClientIDMode = ClientIDMode.AutoID,
                            Text         = options.WarningText
                        };
                        div.Controls.Add(warningBlock);
                    }

                    div.Controls.Add(attributeControl);
                }
                else
                {
                    controls.Add(attributeControl);
                }
            }

            if (options.ShowPrePostHtml)
            {
                if (this.PostHtml.IsNotNullOrWhiteSpace())
                {
                    controls.Add(new Literal {
                        Text = this.PostHtml
                    });
                }
            }

            if (options.SetValue)
            {
                FieldType.Field.SetEditValue(attributeControl, QualifierValues, options.Value);
            }

            return(attributeControl);
        }
示例#3
0
        /// <summary>
        /// Adds the control.
        /// </summary>
        /// <param name="controls">The controls.</param>
        /// <param name="value">The value.</param>
        /// <param name="validationGroup">The validation group.</param>
        /// <param name="setValue">if set to <c>true</c> [set value].</param>
        /// <param name="setId">if set to <c>true</c> [set id].</param>
        /// <param name="required">The required.</param>
        /// <param name="labelText">The label text.</param>
        /// <param name="helpText">The help text.</param>
        /// <param name="warningText">The warning text.</param>
        /// <returns></returns>
        public Control AddControl(ControlCollection controls, string value, string validationGroup, bool setValue, bool setId, bool?required = null, string labelText = null, string helpText = null, string warningText = null)
        {
            if (labelText == null)
            {
                labelText = this.Name;
            }

            if (helpText == null)
            {
                helpText = this.Description;
            }

            Control attributeControl = this.FieldType.Field.EditControl(QualifierValues, setId ? string.Format("attribute_field_{0}", this.Id) : string.Empty);

            if (attributeControl != null)
            {
                if (setId)
                {
                    attributeControl.ClientIDMode = ClientIDMode.AutoID;
                }

                // If the control is a RockControl
                var rockControl = attributeControl as IRockControl;
                if (rockControl != null)
                {
                    rockControl.Label           = labelText;
                    rockControl.Help            = helpText;
                    rockControl.Warning         = warningText;
                    rockControl.Required        = required.HasValue ? required.Value : this.IsRequired;
                    rockControl.ValidationGroup = validationGroup;

                    controls.Add(attributeControl);
                }
                else
                {
                    bool renderLabel   = !string.IsNullOrEmpty(labelText);
                    bool renderHelp    = !string.IsNullOrWhiteSpace(helpText);
                    bool renderWarning = !string.IsNullOrWhiteSpace(warningText);

                    if (renderLabel || renderHelp || renderWarning)
                    {
                        DynamicControlsHtmlGenericControl div = new DynamicControlsHtmlGenericControl("div");
                        div.ID = $"_formgroup_div_{this.Id}";
                        controls.Add(div);

                        div.Controls.Clear();
                        div.AddCssClass("form-group");
                        if (this.IsRequired)
                        {
                            div.AddCssClass("required");
                        }

                        div.ClientIDMode = ClientIDMode.AutoID;

                        if (renderLabel)
                        {
                            Label label = new Label();
                            label.ID = $"_label_{this.Id}";
                            div.Controls.Add(label);
                            label.ClientIDMode        = ClientIDMode.AutoID;
                            label.Text                = labelText;
                            label.CssClass            = "control-label";
                            label.AssociatedControlID = attributeControl.ID;
                        }

                        if (renderHelp)
                        {
                            var helpBlock = new Rock.Web.UI.Controls.HelpBlock();
                            helpBlock.ID = $"_helpBlock_{this.Id}";
                            div.Controls.Add(helpBlock);
                            helpBlock.ClientIDMode = ClientIDMode.AutoID;
                            helpBlock.Text         = helpText;
                        }

                        if (renderWarning)
                        {
                            var warningBlock = new Rock.Web.UI.Controls.WarningBlock();
                            warningBlock.ID = $"_warningBlock_{this.Id}";
                            div.Controls.Add(warningBlock);
                            warningBlock.ClientIDMode = ClientIDMode.AutoID;
                            warningBlock.Text         = warningText;
                        }

                        div.Controls.Add(attributeControl);
                    }
                    else
                    {
                        controls.Add(attributeControl);
                    }
                }

                if (setValue)
                {
                    this.FieldType.Field.SetEditValue(attributeControl, QualifierValues, value);
                }
            }

            return(attributeControl);
        }