【问题标题】:How to retain new controls after PostBack?PostBack 后如何保留新控件?
【发布时间】:2017-10-05 17:56:06
【问题描述】:

我正在创建一个允许用户创建自己的表单的网络应用程序。但是一旦添加了控件(例如带有控件的新标签),它将在我尝试将另一个对象添加到表单时被删除。 这是创建新项目的按钮的代码。

protected void createFormButton_Click(object sender, EventArgs e)
    {

       ////titles is the div id of where i want to insert the title label
        var titlelabel = new Label();
        titlelabel.Text = textboxForTitle.Text;
        titles.Controls.Add(titlelabel);
        controls.Add(titlelabel);

        if (optionsDropdown.SelectedValue == "Checkbox")
        {
           //elements is the div id of where i want to insert the control
            var newControl = new CheckBox();
            newControl.CssClass = "checkbox";
            newControl.Checked = true;
            elements.Controls.Add(newControl);
            controls.Add(newControl);
        }

        else if (optionsDropdown.SelectedValue == "Textbox")
        {
           //elements is the div id of where i want to insert the control
            var newControl = new TextBox();
            newControl.Text = "this is some text on the new box";
            newControl.CssClass = "form-control";
            elements.Controls.Add(newControl);
        }

        else if (optionsDropdown.SelectedValue == "Dropdown")
        {
           //elements is the div id of where i want to insert the control
            var newControl = new DropDownList();
            newControl.Items.Add("one");
            newControl.Items.Add("two");
            newControl.Items.Add("three");
            newControl.CssClass = "form-control";
            elements.Controls.Add(newControl);
        }
    }

如何保存新控件,以便每次单击按钮时,它们之前的控件不会在回发时被删除?

【问题讨论】:

  • I am creating a web app that allows the user to create their own forms. - 可以在 ASP.NET 中完成,但 MVC 使这样的任务更容易。如果您刚刚开始使用此应用程序,您可能会考虑改用这条路线。

标签: c# asp.net webforms


【解决方案1】:

动态添加控件时,它们会在每次回发时被删除。现在,如果您遵循 asp 页面的生命周期,您会注意到视图状态(保存来自客户端对象的所有数据的变量)位于 page.init 之后。因此,当在 asp 中使用动态添加的控件时,您需要在 page.init 事件中的每个回发时重新创建。然后将视图状态加载到这些控件中。我这样做的方式是我将每个控件保存在会话中的列表(控件)中,并将它们添加到 page.init 的占位符中

【讨论】:

    【解决方案2】:

    您可以创建一个类级别的变量,它是一个控件列表,而不是向内置控件集合中添加一个项目。在您的情况下,您需要两个,因为您正在向页面和元素 div 添加控件。这些列表将在回发中持续存在,因此每次您都可以在方法结束时通过 AddRange 相应地重新填充页面控件。

    注意:代码未经测试。

    List<Control> pageControls = new List<Control>();
    List<Control> elementControls = new List<Control>();
    
    protected void createFormButton_Click(object sender, EventArgs e)
        {
    
           ////titles is the div id of where i want to insert the title label
            var titlelabel = new Label();
            titlelabel.Text = textboxForTitle.Text;
            titles.Controls.Add(titlelabel);
            pageControls.Add(titlelabel);
    
            if (optionsDropdown.SelectedValue == "Checkbox")
            {
               //elements is the div id of where i want to insert the control
                var newControl = new CheckBox();
                newControl.CssClass = "checkbox";
                newControl.Checked = true;
                elements.Controls.Add(newControl);
                pageControls.Add(newControl);
            }
    
            else if (optionsDropdown.SelectedValue == "Textbox")
            {
               //elements is the div id of where i want to insert the control
                var newControl = new TextBox();
                newControl.Text = "this is some text on the new box";
                newControl.CssClass = "form-control";
                elementControls.Add(newControl);
            }
    
            else if (optionsDropdown.SelectedValue == "Dropdown")
            {
               //elements is the div id of where i want to insert the control
                var newControl = new DropDownList();
                newControl.Items.Add("one");
                newControl.Items.Add("two");
                newControl.Items.Add("three");
                newControl.CssClass = "form-control";
                elementControls.Add(newControl);
            }
            Controls.AddRange(pageControls);
            elements.Controls.AddRange(elementControls);
        }
    

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 2011-12-23
      • 1970-01-01
      • 2012-08-13
      • 2023-03-05
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多