private BaseFieldTemplate LoadBaseFieldTemplateFromControlType(ControlType controlType) { BaseFieldTemplate ctrl = null; if (!String.IsNullOrEmpty(controlType.Assembly) && !String.IsNullOrEmpty(controlType.Class)) { ctrl = (BaseFieldTemplate)Activator.CreateInstance(controlType.Assembly, controlType.Class).Unwrap(); } return(ctrl); }
public void AddControl(Screen Screen, Section Section, Control parent, Widget control) { try { bool IsControlVisible = true; bool IsControlEditable = true; ControlType controlType = null; ControlType labelControlType = null; BaseFieldTemplate ctrl = null; BaseFieldTemplate readOnlyCtrl = null; System.Web.UI.WebControls.BaseValidator val = null; HtmlGenericControl group = null; HtmlGenericControl labelContainer = null; System.Web.UI.WebControls.Label label = null; HtmlGenericControl controlContainer = null; PlaceHolder groupContainer = null; Control help = null; IsControlVisible = DetermineControlVisibility(control, IsControlVisible); IsControlEditable = DetermineIfControlIsEditable(control, IsControlVisible, IsControlEditable); //setup overall container groupContainer = new PlaceHolder(); groupContainer.ID = String.Format("{0}_GroupContainer", control.Name); groupContainer.Visible = IsControlVisible; //setup control group if needed if (!String.IsNullOrEmpty(control.ControlGroupElement)) { group = new HtmlGenericControl(control.ControlGroupElement); group.ID = String.Format("{0}_FormGroup", control.Name); if (!String.IsNullOrEmpty(control.ControlGroupCssClass)) { group.Attributes.Add("class", control.ControlGroupCssClass); } } //setup label container if needed if (!String.IsNullOrEmpty(control.LabelContainerElement)) { labelContainer = new HtmlGenericControl(control.LabelContainerElement); labelContainer.ID = String.Format("{0}_LabelContainer", control.Name); if (!String.IsNullOrEmpty(control.LabelContainerCssClass)) { labelContainer.Attributes.Add("class", control.LabelContainerCssClass); } } //setup control container if needed if (!String.IsNullOrEmpty(control.ControlContainerElement)) { controlContainer = new HtmlGenericControl(control.ControlContainerElement); controlContainer.ID = String.Format("{0}_ControlContainer", control.Name); if (!String.IsNullOrEmpty(control.ControlContainerCssClass)) { controlContainer.Attributes.Add("class", control.ControlContainerCssClass); } } //setup actual control controlType = ControlType.GetControlType(control); if (controlType == null) { throw new ApplicationException(String.Format("Control of type {0} could not be found in configuration", control.Type)); } if (!IsControlEditable) { labelControlType = ControlType.GetControlType("Label"); } if (controlType != null) { //main edit control ctrl = LoadBaseFieldTemplateFromControlType(controlType); ctrl.ID = control.Name; ctrl.ResourceKeyPrefix = String.Format("Screen.Sections.{0}.Control", Section.Name); BaseFieldTemplate renderControl = null; //alternate edit control in read only mode if (!IsControlEditable) { readOnlyCtrl = LoadBaseFieldTemplateFromControlType(labelControlType); readOnlyCtrl.ID = String.Format("{0}_ReadOnly_Label", control.Name); } if (IsControlEditable) { renderControl = ctrl; } else { renderControl = readOnlyCtrl; } //label label = new System.Web.UI.WebControls.Label(); label.ID = String.Format("{0}_Label", control.Name); if (!String.IsNullOrEmpty(control.LabelCssClass)) { string labelCss = String.Format("{0}", control.LabelCssClass); if (control.IsRequired) { labelCss += " required required-label"; } label.CssClass = labelCss; } if (!String.IsNullOrEmpty(control.Label)) { string labelText = GetGlobalResourceString(String.Format("Control.{0}.Label", control.Name), control.Label); labelText += ":"; label.Controls.Add(new LiteralControl(labelText)); } //help text if (!String.IsNullOrEmpty(control.HelpText)) { string helpText = GetGlobalResourceString(String.Format("Control.{0}.HelpText", control.Name), control.HelpText); if (String.IsNullOrEmpty(control.HelpTextElement)) { help = new LiteralControl(control.HelpText); } else { HtmlGenericControl helpControl = new HtmlGenericControl(control.HelpTextElement); helpControl.InnerHtml = control.HelpText; if (!String.IsNullOrEmpty(control.HelpTextCssClass)) { helpControl.Attributes.Add("class", control.HelpTextCssClass); } help = helpControl; } } //validation controls List <System.Web.UI.WebControls.BaseValidator> validators = new List <System.Web.UI.WebControls.BaseValidator>(); if (IsControlEditable) { //custom validators foreach (CodeTorch.Core.BaseValidator validator in control.Validators) { System.Web.UI.WebControls.BaseValidator validatorControl = GetValidator(control, validator); validators.Add(validatorControl); } } //assign base control, section and screen references ctrl.Widget = control; if (!IsControlEditable) { readOnlyCtrl.Widget = CreateLabelControlFromWidget(control); readOnlyCtrl.Value = ctrl.Value; readOnlyCtrl.SetDisplayText(ctrl.DisplayText); } ctrl.Section = Section; ctrl.Screen = Screen; //now that all controls have been defined we now need to render table html based on rendering mode if (group != null) { if (control.LabelWrapsControl) { AssignControl(group, labelContainer, label); AssignControl(label, controlContainer, renderControl, help); } else { label.AssociatedControlID = renderControl.ClientID; if (control.LabelRendersBeforeControl) { AssignControl(group, labelContainer, label); AssignControl(group, controlContainer, renderControl, help); } else { AssignControl(group, controlContainer, renderControl, help); AssignControl(group, labelContainer, label); } } } else { Control g = groupContainer;// (parent != null) ? (Control)parent : (Control)this.ContentPlaceHolder; if (control.LabelWrapsControl) { AssignControl(g, labelContainer, label); AssignControl(label, controlContainer, renderControl, help); } else { label.AssociatedControlID = renderControl.ClientID; if (control.LabelRendersBeforeControl) { AssignControl(g, labelContainer, label); AssignControl(g, controlContainer, renderControl, help); } else { AssignControl(g, controlContainer, renderControl, help); AssignControl(g, labelContainer, label); } } } if (parent != null) { if (group != null) { groupContainer.Controls.Add(group); } parent.Controls.Add(groupContainer); } else { if (group != null) { groupContainer.Controls.Add(group); } this.ContentPlaceHolder.Controls.Add(groupContainer); } string requiredErrorMessage = String.Format("{0} is required.", control.Label); string requiredErrorMessageResourceValue = GetGlobalResourceString(String.Format("Control.{0}.IsRequired.ErrorMessage", control.Name), requiredErrorMessage); //get container for validation Control valContainer = ((Control)controlContainer ?? ( (Control)group ?? ( ((Control)parent ?? (Control)this.ContentPlaceHolder) ) ) ); if (!String.IsNullOrEmpty(control.Name)) { if (ctrl.SupportsValidation()) { val = ctrl.GetRequiredValidator(control, IsControlEditable, requiredErrorMessageResourceValue); valContainer.Controls.Add(val); if (IsControlEditable) { //custom validators foreach (System.Web.UI.WebControls.BaseValidator validator in validators) { valContainer.Controls.Add(validator); } } } } } else { string ErrorMessageFormat = "<span style='color:red'>ERROR - Could not load control {0} - {1} - control type returned null object</span>"; string ErrorMessages = String.Format(ErrorMessageFormat, control.Name, control.Type); this.ContentPlaceHolder.Controls.Add(new LiteralControl(ErrorMessages)); } } catch (Exception ex) { this.ContentPlaceHolder.Controls.Add(new LiteralControl(ex.Message)); } }