C# 在运行时添加控件Form Builder

C# 在运行时添加控件Form Builder,c#,asp.net,C#,Asp.net,我使用下面的代码向面板添加项目,但问题是它只允许我添加一个控件实例,我希望能够向面板添加任意数量的项目,我认为下面的代码可以实现这一点 if (ctrlType.SelectedValue == "TextBox") { listElements.Add(new XElement(@"TextBox", new XElement("name"), new XElement("Type",

我使用下面的代码向面板添加项目,但问题是它只允许我添加一个控件实例,我希望能够向面板添加任意数量的项目,我认为下面的代码可以实现这一点

 if (ctrlType.SelectedValue == "TextBox")
        {
            listElements.Add(new XElement(@"TextBox", new XElement("name"),
                                  new XElement("Type", "System.String"),
                                    new XElement("displayName", this.txtTitle.Text.ToString()),
                                    new XElement("length", txtMaxLength.Text.ToString()),
                                     new XElement("key", false),
                                     new XElement("required",  chkRequired.Checked)));


            TextBoxUserControl tb2 =
                         (TextBoxUserControl)LoadControl(@"~\UserControls\TextBoxUserControl.ascx");
            tb2.XMLText = listElements;
            tb2.Text = txtTitle.Text;
            tb2.Name = "TextBox" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
            pnlControls.Controls.Add(tb2);

        }

        if (ctrlType.SelectedValue == "DropDown" && ctrlSource.SelectedValue == "0")   {
            listElements.Add(new XElement(@"ClassficationEnum", new XElement("name", "TestForm"),
                new XElement("Guid", "1f77f0ce-9e43-340f-1fd5-b11cc36c9cba"),
                                     new XElement("Type", "System.String"),
                                       new XElement("displayName", this.txtTitle.Text.ToString()),
                                       new XElement("length", txtMaxLength.Text.ToString()),
                                        new XElement("key", false),
                                        new XElement("required",  chkRequired.Checked)));


            Classfication clafficationDp =
                    (Classfication)LoadControl(@"~\UserControls\Classfication.ascx");
            clafficationDp.ID = "clafficationDp" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
            clafficationDp.Text = txtTitle.Text;
            pnlControls.Controls.Add(clafficationDp);



        }
        else if (ctrlType.SelectedValue == "DropDown" && ctrlSource.SelectedValue == "1")
        {
            listElements.Add(new XElement(@"SourceEnum", new XElement("name", "TestForm"),
                new XElement("Guid", "5d59071e-69b3-7ef4-6dee-aacc5b36d898.xml"),
                                     new XElement("Type", "System.String"),
                                       new XElement("displayName", this.txtTitle.Text.ToString()),
                                       new XElement("length", txtMaxLength.Text.ToString()),
                                        new XElement("key", false),
                                        new XElement("required", chkRequired.Checked)));



            SourceEnum dpsource =
                         (SourceEnum)LoadControl(@"~\UserControls\SourceEnum.ascx");
            dpsource.ID = "DropList" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
            dpsource.Text = txtTitle.Text;
            pnlControls.Controls.Add(dpsource);

        }
        UpdateActiveControl("Test", "Form Name", "Test Control", listElements);



        pnlControls.Controls.Add(new LiteralControl("<br />"));
  //      formControls = formGen.GetFormDataFromService();
        foreach (XElement formControl in listElements)
         {

             FormStructure frmstructure = new FormStructure();

             frmstructure.displayname = formControl.Element("displayName").Value;
             frmstructure.Required = Convert.ToBoolean(formControl.Element("required").Value);
             frmstructure.length = formControl.Element("length").Value;
             frmstructure.ControlType = formControl.Element("Type").Value;                


             formControls.Add(frmstructure);
         }
if(ctrlType.SelectedValue==“文本框”)
{
添加(新的XElement(@“TextBox”),新的XElement(“名称”),
新的XElement(“Type”、“System.String”),
新的XElement(“displayName”,this.txtTitle.Text.ToString()),
新的XElement(“length”,txtMaxLength.Text.ToString()),
新XElement(“key”,false),
新元素(“必需”,chkRequired.Checked));
TextBoxUserControl tb2=
(TextBoxUserControl)LoadControl(@“~\UserControls\TextBoxUserControl.ascx”);
tb2.XMLText=列表元素;
tb2.Text=txtTitle.Text;
tb2.Name=“TextBox”+“”+ctrlSource.SelectedValue.ToString()+“”+Guid.NewGuid().ToString();
pnlControls.Controls.Add(tb2);
}
如果(ctrlType.SelectedValue==“DropDown”&&ctrlSource.SelectedValue==“0”){
添加(新的XElement(@“classificationenum”),新的XElement(“name”,“TestForm”),
新XElement(“Guid”、“1f77f0ce-9e43-340f-1fd5-b11cc36c9cba”),
新的XElement(“Type”、“System.String”),
新的XElement(“displayName”,this.txtTitle.Text.ToString()),
新的XElement(“length”,txtMaxLength.Text.ToString()),
新XElement(“key”,false),
新元素(“必需”,chkRequired.Checked));
分类分类=
(分类)LoadControl(@“~\UserControls\classification.ascx”);
clafficationDp.ID=“clafficationDp”+“”+ctrlSource.SelectedValue.ToString()+“”+Guid.NewGuid().ToString();
clafficationDp.Text=txtTitle.Text;
pnlControls.Controls.Add(clafficationDp);
}
else if(ctrlType.SelectedValue==“DropDown”&&ctrlSource.SelectedValue==“1”)
{
添加(新的XElement(@“SourceEnum”),新的XElement(“名称”,“测试表单”),
新XElement(“Guid”、“5d59071e-69b3-7ef4-6dee-aacc5b36d898.xml”),
新的XElement(“Type”、“System.String”),
新的XElement(“displayName”,this.txtTitle.Text.ToString()),
新的XElement(“length”,txtMaxLength.Text.ToString()),
新XElement(“key”,false),
新元素(“必需”,chkRequired.Checked));
SourceEnum dpsource=
(SourceEnum)LoadControl(@“~\UserControls\SourceEnum.ascx”);
dpsource.ID=“DropList”+“”+ctrlSource.SelectedValue.ToString()+“”+Guid.NewGuid().ToString();
dpsource.Text=txtTitle.Text;
pnlControls.Controls.Add(dpsource);
}
UpdateActiveControl(“测试”、“表单名称”、“测试控件”、列表元素);
pnlControls.Controls.Add(新的LiteralControl(“
”); //formControls=formGen.GetFormDataFromService(); foreach(列表元素中的XElement formControl) { FormStructure frmstructure=新的FormStructure(); frmstructure.displayname=formControl.Element(“displayname”).Value; frmstructure.Required=Convert.ToBoolean(formControl.Element(“Required”).Value); frmstructure.length=formControl.Element(“长度”).Value; frmstructure.ControlType=formControl.Element(“Type”).Value; formControls.Add(FRM结构); }
这是我加载表单控件详细信息的地方

 public partial class _default : System.Web.UI.Page
{

    PortalContext portalContext = new PortalContext();
    DataAccess da = new DataAccess();
    FormGenerator formGen = new FormGenerator();
    List<FormStructure> formControls = new List<FormStructure>();
    List<XElement> listElements = new List<XElement>();

    protected void Page_Load(object sender, EventArgs e)
    {
        da.SqlInstanceName = "CDDEVSVR-SQL";
        da.PortalDatabaseName = "PortalCms";
        da.IntegratedSecurity = true;
        SetEntityContextConnectionStrings();

        if (!IsPostBack)
        {
            formControls = formGen.GetFormDataFromService();
        }
        foreach (FormStructure formControl in formControls)
        {


            if (formControl.ControlType == "TextBox")
            {
                listElements.Add(new XElement(@"TextBox", new XElement("name"),
                                new XElement("Type", "System.String"),
                                  new XElement("displayName", this.txtTitle.Text.ToString()),
                                  new XElement("length", txtMaxLength.Text.ToString()),
                                   new XElement("key", false),
                                   new XElement("required", chkRequired.Checked)));



                TextBoxUserControl textBoxControl =
                    (TextBoxUserControl)LoadControl(@"~\UserControls\TextBoxUserControl.ascx");
                textBoxControl.XMLText = listElements;
                textBoxControl.Text = formControl.displayname;

                pnlControls.Controls.Add(textBoxControl);
                pnlControls.Controls.Add(new LiteralControl("<br />"));


            }

            if (formControl.ControlType == "DropDown")
            {
                listElements.Add(new XElement(@"ClassficationEnum", new XElement("name", "TestForm"),
                     new XElement("Guid", "1f77f0ce-9e43-340f-1fd5-b11cc36c9cba"),
                                          new XElement("Type", "System.String"),
                                            new XElement("displayName", this.txtTitle.Text.ToString()),
                                            new XElement("length", txtMaxLength.Text.ToString()),
                                             new XElement("key", false),
                                             new XElement("required", chkRequired.Checked)));


                SourceEnum dpsource =
                     (SourceEnum)LoadControl(@"~\UserControls\SourceEnum.ascx");
                dpsource.ID = "DropList" + " " + ctrlSource.SelectedValue.ToString() + " " + Guid.NewGuid().ToString();
                dpsource.Text = formControl.displayname;
                pnlControls.Controls.Add(dpsource);




            }
        }
    }
public分部类\u默认值:System.Web.UI.Page
{
PortalContext PortalContext=新的PortalContext();
DataAccess da=新的DataAccess();
FormGenerator formGen=新的FormGenerator();
List formControls=新列表();
列表元素=新列表();
受保护的无效页面加载(对象发送方、事件参数e)
{
da.SqlInstanceName=“CDDEVSVR-SQL”;
da.PortalDatabaseName=“PortalCms”;
da.IntegratedSecurity=true;
SetEntityContextConnectionString();
如果(!IsPostBack)
{
formControls=formGen.GetFormDataFromService();
}
foreach(formControls中的FormStructure formControl)
{
如果(formControl.ControlType==“文本框”)
{
添加(新的XElement(@“TextBox”),新的XElement(“名称”),
新的XElement(“Type”、“System.String”),
新的XElement(“displayName”,this.txtTitle.Text.ToString()),
新的XElement(“length”,txtMaxLength.Text.ToString()),
新XElement(“key”,false),
新元素(“必需”,chkRequired.Checked));
TextBoxUserControl textBoxControl=
(TextBoxUserControl)LoadControl(@“~\UserControls\TextBoxUserControl.ascx”);
textBoxControl.XMLText=列表元素;
Text=formControl.displayname;
pnlControls.Controls.Add(textBoxControl);
pnlControls.Controls.Add(新的LiteralControl(“
”); } 如果(formControl.ControlType==“下拉列表”) { 添加(新的XElement(@“classificationenum”),新的XElement(“name”,“TestForm”), 新元素(“Guid”、“1f77f0ce-9e43-340f-1fd5-b11cc36c9cba”
private List<FormStructure> GetFormStructureStore () {
  IList<FormStructure> formControls = (IList<FormStructure>)HttpContext.Current.Cache["FormControlsKey"];
  if(formControls == null)
  {
    formControls = new List<FormStructure>();
    HttpContext.Current.Cache.Add(formControls);
  }
  return formControls;
}

protected override void OnInit(EventArgs e)
{
  IList<FormStructure> formControls = GetFormStructureStore();
  // load the controls and add them to the Controls collection
  // ...
}
//List<FormStructure> formControls = new List<FormStructure>();
formGen.GetFormDataFromService();