【发布时间】: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 使这样的任务更容易。如果您刚刚开始使用此应用程序,您可能会考虑改用这条路线。