2017-10-05 8 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イベントで各ポストバックを再作成する必要があります。その後、ビューステートがコントロールにロードされます。私がそれを行う方法は、コントロールのリスト(コントロールの)をセッション中に作成し、page.initのプレースホルダに追加することです。

-2

コントロールのリストであるクラスレベルの変数を作成できます組み込みのコントロールコレクションにアイテムを追加するよりも簡単です。あなたの場合は、ページと要素divの両方にコントロールを追加するので、2つ必要です。これらのリストは、ポストバックをまたいで存続するため、メソッドの最後に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); 
    } 
関連する問題