私は自分のフォームを作成できるWebアプリケーションを作成しています。一度コントロールが追加されると(たとえばコントロールの新しいラベルなど)、フォームに別のオブジェクトを追加しようとすると削除されます。 これは、新しい項目を作成するボタンのコードです。PostBackの後に新しいコントロールを保持する方法は?
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);
}
}
どのように彼らは、以前のコントロールがポストバックに削除されません]をクリックし、各ボタンでので、私は、新しいコントロールを保存することができますか?
「ユーザーが自分のフォームを作成できるようにするWebアプリケーションを作成しています。」 - ASP.NETで実行することができますが、MVCではこのような作業が簡単です。このアプリをちょうど開始している場合は、代わりにそのルートに行くと考えるかもしれません。 – NightOwl888