在运行时表单生成器添加控件

时间:2014-07-16 14:39:17

标签: c# asp.net

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

 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);
         }

这是我加载表单控件的详细信息

 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);




            }
        }
    }

2 个答案:

答案 0 :(得分:1)

我做了一些业余级别的工作,以类似于集合的方式动态地将用户控件添加到ASP.NET Web表单,并且能够使其正常工作。如果您的问题是@rene建议您只看到一个控件通过回发持续存在,那可能是因为this question answer和@ovm建议中描述的页面呈现过程问题;每个页面渲染必须重新生成控件,我能够使用存储在for-purpose会话变量中的数组来做到这一点。

也就是说,在处理这个功能时,一些博客文章表明这​​不是一个很棒的想法,并且看到它在页面生成和渲染中的工作方式,我同意。我不希望有一个轻松的时间来激活页面的动态部分或以后改进它。常见的(可能是两个博客)解决方法是设计为已经可能需要的所有控件已经内置到页面中,然后隐藏并且不提供给定页面不需要的那些控件渲染。 HTH

答案 1 :(得分:0)

您必须跟踪已添加到面板中的所有控件,并在回发时重新添加它们。

如果这些控件应该是Event-Aware,则应在页面生命周期的OnInit阶段添加它们。

<强>更新

您不能保证formControls变量的初始化会在多个请求中保留每个回发。您可以做的是跟踪HttpContext.Current.Cache中的FormStructure实例,并根据缓存中的FormStructures在OnInit阶段重新创建控件。

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
  // ...
}

现在可以使用此方法从Cache获取formControls并删除Page level变量。

//List<FormStructure> formControls = new List<FormStructure>();

另外,你能告诉我们什么

formGen.GetFormDataFromService();

实际上正在回归?

我问这个是因为即使formControls的内容在Session中的某个时刻丢失了,如果GetFormDataFromService()返回多个FormStructures,那些实际上应该是可见的。

相关问题