private void AddEditControlsToSet(HtmlGenericControl dynamicDiv, Element element)
        {
            //create edit buttons
            HtmlGenericControl btnRemove = new HtmlGenericControl("input");
            btnRemove.Attributes.Add("type", "button");
            btnRemove.Attributes.Add("class", "remove");
            btnRemove.Attributes.Add("value", "X");
            dynamicDiv.Controls.Add(btnRemove);

            CheckBox chkValidate = new CheckBox();
            chkValidate.CssClass = "doValidate";
            chkValidate.Text = "Validate";
            chkValidate.ID = "btn_remove_" + element.ControlID.ToString();
            chkValidate.ClientIDMode = ClientIDMode.Static;
            dynamicDiv.Controls.Add(chkValidate);
        }
        private void BuildFormObjects(Form newForm, DataTable formData, DataTable formContainers, DataTable formElements, DataTable formChildElements, DataTable elementActions, DataTable formActions, DataTable elementBehaviors)
        {
            //Instantiate new lists
            newForm.ContainerNames = new List<string>();
            newForm.Elements = new List<Element>();

            #region Validation

            #region FormData

            //Validation
            if (!formData.Rows[0]["Name"].Equals(null))
                newForm.Name = formData.Rows[0]["Name"].ToString();

            if (!formData.Rows[0]["ItemID"].Equals(null))
                newForm.SitecoreID = formData.Rows[0]["ItemID"].ToString();

            if (!formData.Rows[0]["CSSClass"].Equals(null))
                newForm.CssClass = formData.Rows[0]["CSSClass"].ToString();

            if (!formData.Rows[0]["Header"].Equals(null))
                newForm.Header = formData.Rows[0]["Header"].ToString();

            #endregion

            #region FormContainer

            newForm.ContainerNames = new List<string>();
            foreach (DataRow row in formContainers.Rows)
            {
                //Validation
                if (!row["ContainerID"].Equals(null))
                    newForm.ContainerNames.Add(row["ContainerID"].ToString());
            }

            #endregion

            #region FormElement

            foreach (DataRow row in formElements.Rows)
            {
                Element element = new Element();

                //Validation
                if (!row["FormControl_ID"].Equals(null))
                    element.FormControlID = Convert.ToInt32(row["FormControl_ID"]);
                if (!row["LabelName"].Equals(null))
                    element.LabelName = row["LabelName"].ToString();
                if (!row["OrderNumber"].Equals(null))
                    element.OrderNumber = Convert.ToInt32(row["OrderNumber"].ToString());
                if (!row["ContainerName"].Equals(null))
                    element.ControllerName = row["ContainerName"].ToString();
                if (!row["ElementType"].Equals(null))
                    element.Type = row["ElementType"].ToString();
                if (!row["Validate"].Equals(null))
                    element.Validate = Convert.ToBoolean(row["Validate"].ToString());
                if (!row["ControlName"].Equals(null))
                    element.ControlName = row["ControlName"].ToString();
                if (!row["AprimoFieldName"].Equals(null))
                    element.AprimoName = row["AprimoFieldName"].ToString();
                if (!row["ControlList_ID"].Equals(null))
                    element.ControlID = (int)row["ControlList_ID"];

                #region ChildElements

                if (element.Type.ToLower().Equals("group"))
                {
                    //validation for element type group name
                    if (!row["GroupName"].Equals(null))
                        element.GroupName = row["GroupName"].ToString();

                    //iteration step for child element within the group element type
                    element.ChildElements = formChildElements.AsEnumerable().Select(child => new Element
                    {
                        FormControlID   = child.Field<int>("FormControl_ID"),
                        ControlID       = child.Field<int>("ControlList_ID"),
                        LabelName       = child.Field<string>("Text"),
                        ControlValue    = child.Field<string>("Value"),
                    }).ToList();
                }

                #endregion

                #region Action

                ElementAction action = new ElementAction();
                element.Actions = new List<ElementAction>();

                for (int i = 0; i < elementActions.Rows.Count; i++)
                {
                    //Validation
                    if (!elementActions.Rows[i]["ActionName"].Equals(null))
                    {
                        element.Actions.Add(new ElementAction { Action = elementActions.Rows[i]["ActionName"].ToString()});
                        element.Actions.Add(action);
                    }
                }

                #region FormActions
                for (int i = 0; i < formActions.Rows.Count; i++)
                {
                    //Validation
                    if (!formActions.Rows[i]["ECASReturnURL"].Equals(null))
                    {
                        element.Actions.Add(new ElementAction { ActionParameters =new KeyValuePair<string,string>("ECAS Login Required", formActions.Rows[i]["ECASReturnURL"].ToString())});
                        element.Actions.Add(action);
                    }
                }

                #endregion

                #endregion

                #region Behaviors

                foreach (DataRow behaviorRow in elementBehaviors.Rows)
                {
                    ElementBehavior elementBehavior = new ElementBehavior();

                    //Validation
                    if (behaviorRow["BehaviorName"].Equals(null))
                    {
                        elementBehavior.Behavior = behaviorRow["BehaviorName"].ToString();
                        element.Behaviors.Add(elementBehavior);
                    }
                }

                #endregion

                //add element to list of elements..
                newForm.Elements.Add(element);
            }

            #endregion

            #endregion

            //Build out HTML Objects...
            SetHTMLObjects(newForm);
        }
        private void SetHtmlChildObjectsProperties(Element parent, Element child, ref HtmlGenericControl dynamicChildDiv)
        {
            //validation for child element type..
            if(parent.ControlName.ToLower().Contains("checkboxes")){
                CheckBox newCheckBox = new CheckBox();
                newCheckBox.ID = "chk" + child.LabelName.ToLower();
                newCheckBox.ClientIDMode = ClientIDMode.Static;
                newCheckBox.Text = child.LabelName;
                newCheckBox.Attributes.Add("text", child.LabelName);
                newCheckBox.Attributes.Add("group", parent.GroupName);

                //Add control to the child div..
                dynamicChildDiv.Controls.Add(newCheckBox);
            }

            if(parent.ControlName.ToLower().Contains("radio")){
                RadioButton newRadioButton = new RadioButton();
                newRadioButton.ID = "rb" + child.LabelName.ToLower();
                newRadioButton.Text = child.LabelName;
                newRadioButton.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                newRadioButton.Attributes.Add("text", child.LabelName);
                newRadioButton.Attributes.Add("group", parent.GroupName);

                //Add control to the child div...
                dynamicChildDiv.Controls.Add(newRadioButton);
            }
        }
        private void SetHtmlObjectsProperties(Element element, ref HtmlGenericControl dynamicDiv, ref HtmlGenericControl dynamicSpan, ref HtmlGenericControl dynamicChildDiv)
        {
            //Textbox
            if (element.Type.ToLower().Equals("textbox")){
                TextBox newTexbox = new TextBox();

                newTexbox.ID = "txt" + element.AprimoName.ToLower().Replace(" ", "");
                newTexbox.ClientIDMode = ClientIDMode.Static;
                newTexbox.Attributes.Add("validate", element.Validate.ToString());
                newTexbox.Attributes.Add("controlName", element.ControlName.ToLower().ToString());
                newTexbox.Attributes.Add("apr", element.AprimoName);

                //remove id
                if (newTexbox.ID.Equals("txtorg"))
                    newTexbox.ID = "company";

                //update div and span elements
                dynamicDiv.ID = element.LabelName;
                dynamicSpan.InnerText = element.LabelName;

                //add span to div...
                dynamicDiv.Controls.Add(dynamicSpan);
                dynamicDiv.Controls.Add(newTexbox);

                AddEditControlsToSet(dynamicDiv, element);

                //Find and store the controls in the correct div..
                pnlContainer.FindControl("pnl" + element.ControllerName.ToLower()).Controls.Add(dynamicDiv);
            }
            //Dropdown
            if (element.Type.ToLower().Equals("dropdownlist")){
                DropDownList newDropDownList = new DropDownList();
                DataTable constantValues = data.GetConstantValues(element.ControlID);
                FormGeneratorTools.BindObject(newDropDownList, constantValues, "Text", "Value", "-Select-");

                newDropDownList.ClientIDMode = ClientIDMode.Static;
                newDropDownList.ID = "ddl" + element.AprimoName;
                newDropDownList.Attributes.Add("validate", element.Validate.ToString());
                newDropDownList.Attributes.Add("controlName", element.ControlName.ToLower().ToString());
                newDropDownList.Attributes.Add("apr", element.AprimoName);

                //update span text value
                dynamicDiv.ID = element.LabelName;
                dynamicSpan.InnerText = element.LabelName;

                //add span to div
                dynamicDiv.Controls.Add(dynamicSpan);
                dynamicDiv.Controls.Add(newDropDownList);

                AddEditControlsToSet(dynamicDiv, element);

                //Find and store the controls in the correct div...
                pnlContainer.FindControl("pnl" + element.ControllerName.ToLower()).Controls.Add(dynamicDiv);
            }

            //Checkbox list
            if (element.Type.ToLower().Equals("checkbox")){
                CheckBox newCheckbox = new CheckBox();
                newCheckbox.ID = "chk" + element.AprimoName;
                newCheckbox.Attributes.Add("validate", element.Validate.ToString());
                newCheckbox.Attributes.Add("controlName", element.ControlName.ToLower().ToString());
                newCheckbox.Attributes.Add("apr", element.AprimoName);

                //update span text value
                dynamicDiv.ID = element.LabelName;
                dynamicSpan.InnerText = element.LabelName;

                //add span to div
                dynamicDiv.Controls.Add(dynamicSpan);
                dynamicSpan.Controls.Add(newCheckbox);

                //Find and store the controls in the correct div
                pnlContainer.FindControl("pn1" + element.ControllerName.ToLower()).Controls.Add(dynamicDiv);
            }

            //Multi-line
            if (element.Type.ToLower().Equals("multi-line")){
                TextBox newMultiLine = new TextBox();
                newMultiLine.TextMode = TextBoxMode.MultiLine;
                newMultiLine.ID = "txt" + element.AprimoName;
                newMultiLine.Attributes.Add("validate", element.Validate.ToString());
                newMultiLine.Attributes.Add("controlName", element.ControlName.ToLower().ToString());
                newMultiLine.Attributes.Add("apr", element.AprimoName);

                //update span text value
                dynamicDiv.ID = element.LabelName;
                dynamicSpan.InnerText = element.LabelName;

                //add span to div
                dynamicDiv.Controls.Add(dynamicSpan);
                dynamicSpan.Controls.Add(newMultiLine);

                //Find and store the controls in the correct div.
                pnlContainer.FindControl("pn1" + element.ControlName.ToLower().ToString());
            }

            //Button
            if (element.Type.ToLower().Equals("submit")){
                HtmlGenericControl newButton = new HtmlGenericControl("input");
                newButton.ClientIDMode = ClientIDMode.Static;
                newButton.ID = "btn" + element.LabelName;
                newButton.Attributes.Add("value", element.LabelName);
                newButton.Attributes.Add("type", "button");
                newButton.Attributes.Add("controlName", element.ControlName.ToLower().ToString());
                newButton.Attributes.Add("class", "buttonBlue floatfix");
                newButton.Attributes.Add("data-getw_action", "fg submit form");
                newButton.Attributes.Add("data-getw_category=", "esri.forms");

                //Find and store the controls in the correct div
                pnlContainer.FindControl("pnl" + element.ControllerName.ToLower()).Controls.Add(newButton);
            }

            //Group (child items within a element)
            if (element.Type.ToLower().Equals("group")){
                foreach (Element child in element.ChildElements)
                {
                    SetHtmlChildObjectsProperties(element, child, ref dynamicChildDiv);
                }
                pnlContainer.FindControl("pnl" + element.ControllerName.ToLower()).Controls.Add(dynamicChildDiv);
            }
        }
        private Form LoadForm(Esri.FormGenerator.Form newForm, DataTable formData, DataTable formContainers, DataTable formElements, DataTable elementActions, DataTable elementBehaviors)
        {
            //Instantiate new lists
            newForm.ContainerNames = new List<string>();
            newForm.Elements = new List<Element>();

            #region Validation

                #region FormData

                //Validation
                if (!formData.Rows[0]["Name"].Equals(null))
                    newForm.Name = formData.Rows[0]["Name"].ToString();

                if (!formData.Rows[0]["ItemID"].Equals(null))
                    newForm.SitecoreID = formData.Rows[0]["ItemID"].ToString();

                if (!formData.Rows[0]["CSSClass"].Equals(null))
                    newForm.CssClass = formData.Rows[0]["CSSClass"].ToString();

                #endregion

                #region FormContainer

                foreach (DataRow row in formContainers.Rows)
                {
                    //Validation
                    if (!row["ContainerID"].Equals(null))
                        newForm.ContainerNames = new List<string>();
                        newForm.ContainerNames.Add(row["ContainerID"].ToString());
                }

                #endregion

                #region FormElement

                foreach (DataRow row in formElements.Rows)
                {
                    Element element = new Element();

                    //Validation
                    if (!row["LabelName"].Equals(null))
                        element.LabelName = row["LabelName"].ToString();
                    if (!row["OrderNumber"].Equals(null))
                        element.OrderName = Convert.ToInt32(row["OrderNumber"].ToString());
                    if (!row["ContainerName"].Equals(null))
                        element.ControllerName = row["ContainerName"].ToString();
                    if (!row["ElementType"].Equals(null))
                        element.Type = row["ElementType"].ToString();
                    if (!row["Validate"].Equals(null))
                        element.Validate = Convert.ToBoolean(row["Validate"].ToString());
                    if (!row["ControlName"].Equals(null))
                        element.ControlName = row["ControlName"].ToString();

                    #region Action

                    foreach (DataRow elementRow in elementActions.Rows)
                    {
                        ElementAction action = new ElementAction();

                        //Validation
                        if (!elementActions.Rows[0]["ActionName"].Equals(null))
                        {
                            element.Actions = new List<ElementAction>();
                            element.Actions.Add(new ElementAction { Action = elementActions.Rows[0]["ActionName"].ToString() });
                            element.Actions.Add(action);
                        }
                    }

                    #endregion

                    #region Behaviors

                    foreach (DataRow behaviorRow in elementBehaviors.Rows)
                    {
                        ElementBehavior behavior = new ElementBehavior();

                        //Validation
                        if (behaviorRow["BehaviorName"].Equals(null))
                        {
                            behavior.Behavior = behaviorRow["BehaviorName"].ToString();
                            element.Behaviors.Add(behavior);
                        }
                    }

                    #endregion

                    //add element to list of elements..
                    newForm.Elements.Add(element);
                }

                #endregion

            #endregion

            return newForm;
        }