2017-10-05 55 views
0

我正在創建一個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); 
     } 
    } 

我該如何保存新的控件,所以每次點擊按鈕時,他們以前的控件在回發時都不會被刪除?

+0

'我正在創建一個Web應用程序,允許用戶創建自己的窗體。「 - 可以在ASP.NET中完成,但是MVC使得這樣的任務更容易。如果你剛開始這個應用程序,你可能會考慮去那條路線。 – NightOwl888

回答

1

當動態添加控件時,它們在每次回發時被刪除。現在如果你遵循一個asp頁面的生命週期,你會注意到viewstate(包含來自客戶端對象的所有數據的變量)出現在page.init之後。因此,在asp中使用動態添加的控件時,需要在page.init事件的每個回發中重新創建。然後視圖狀態被加載到控件中。我這樣做的方式是我在會話列表(控件)中創建每個控件並將它們添加到頁面的佔位符中.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); 
    } 
相關問題