2011-02-22 26 views
0

我有一個困擾我三天的問題。問題與動態文本框,如何存儲textValues

在我的web表單上我想要一個按鈕,在這個按鈕上點擊我想動態地在asp:PlaceHolder中創建五個文本框。

而且我想,我在這個texBoxes中輸入的值,即使在postBack之後也會保存。用第二個按鈕我想存儲它們。

我已經閱讀過有關頁面,viewState,IsPostBack,...的很多文章的動態創建控件的文章,但我仍然無法編程這個。

我嘗試了幾種方法,但沒有成功。以下是我的「傑作」的最後一個版本。請幫我完成這個任務,因爲它讓我感到噁心。謝謝,馬丁

namespace DynamicCreate 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected System.Web.UI.WebControls.TextBox textBox; 

     private TextBox[] my_dynamicTextBoxes = new TextBox[5]; 
     private string[] textBoxValues = new string[5]; 

     protected void Page_Load(object sender, System.EventArgs e) 
     { 
      btn_save_tb_values.Click += new EventHandler(save_btnClick); 
      but_load_tb.Click += new EventHandler(creat_tb_btnClick); 

      int i = 0; 
      foreach (TextBox tb in my_dynamicTextBoxes) 
      { 
       if (ViewState["c_textBox" + i.ToString()] != null) 
       { 
        tb.Text = (string)ViewState["c_textBox" + i.ToString()]; 
        i++; 
       } 
       else 
       { 
        textBox = new TextBox(); 
        textBox.ID = "c_textBox" + i.ToString(); 
        my_dynamicTextBoxes[i] = textBox; 
        i++; 
       } 
      } 
     } 

     protected void creat_tb_btnClick(object sender, EventArgs e) 
     { 
      int i = 0; 
      foreach (TextBox neww in my_dynamicTextBoxes) 
      { 
       c_placeholder.Controls.Add(neww); 
       c_placeholder.Controls.Add(new LiteralControl("<br>")); 
       ViewState["c_textBox" + i.ToString()] = neww.Text; 
       i++; 
      } 
     } 

     protected void save_btnClick(object sender, EventArgs e) 
     { 
      for (int i = 0; i < 5; i++) {} 
     } 
    } 
} 



<form id="form1" runat="server"> 
     <div> 
      <div> <asp:PlaceHolder ID="c_placeholder" runat="server"></asp:PlaceHolder> </div> 
      <div> <asp:Button runat="server" ID="but_load_tb" Text="Dodaj Polja!!"/>  </div> 
      <div> <asp:Button runat="server" ID="btn_save_tb_values" Text="Izpisi Vrednosti!" /> </div 
     </div> 
    </form> 

回答

0

下面是一個簡單的例子,我想出了將動態地添加5個文本框到一個佔位符當您單擊「創建」按鈕,當你點擊「保存」按鈕,將彈出的值:

的Default.aspx:

<asp:PlaceHolder ID="phButtons" runat="server" /> 
<asp:Button ID="btnCreate" Text="Create" runat="server" onclick="btnCreate_Click" /> 
<asp:Button ID="btnSave" Text="Save" runat="server" onclick="btnSave_Click" /> 

Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (ViewState["buttons"] != null) 
     CreateButtons(); 
} 

protected void btnCreate_Click(object sender, EventArgs e) 
{ 
    ViewState["buttons"] = true; 
    CreateButtons(); 
} 

protected void btnSave_Click(object sender, EventArgs e) 
{ 
    if (ViewState["buttons"] != null) 
    { 
     // Save the button information. 
     foreach (Control ctl in phButtons.Controls) 
     { 
      string x; 
      if (ctl is TextBox) 
       x = (ctl as TextBox).Text; 
     } 
    } 
} 

private void CreateButtons() 
{ 
    for (int iLoop = 0; iLoop < 5; iLoop++) 
    { 
     phButtons.Controls.Add(new TextBox() { ID = "txt" + iLoop }); 
    } 
} 
0

這不會工作的傢伙。

當發生「click」事件時,無法添加需要回發信息(如viewstate等)的控件。只要嘗試在click事件上添加一個按鈕,動態按鈕將不起作用。就是這樣。你必須有一點不同。

從開始創建您的文本框,將它們添加到您的佔位符。

SET它們隱藏起來:mycontrol.visible = false

然後在點擊事件,本質上儘可能多的顯示「myControl」是你想訪問他們,讓他們,設置它們的值並設置其知名度。當「mycontrol」設置爲「初始」時,您將沒有任何問題!

private l as new List(of textbox) 
protected sub onload() 
    for i = 0 to 10 
    dim txt as new textbox 
    txt.visible = false 
    l.add(txt) 
    me.controls.add(txt) 
    end for 
end sub 


protected sub onClick() 
    dim controlsCountNeeded = 4 
    dim q = (from i in me.l where i.visible = false).take(4) 
    for each item in q 
     item.visible = true 
    end for 
end sub 

只是一個簡單的例子-...