2014-12-06 50 views
-1
在運行時添加文本框

in.aspx頁在Asp.net

<form id="MyForm" method="post" runat="server"> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <asp:Panel ID="Panel1" runat="server"> 
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
    </asp:Panel> 
</asp:Content> 
</form> 

在後面的代碼.cs文件

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      //Remove the session when first time page loads. 
      Session.Remove("clicks"); 
     } 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     int rowCount = 0; 

     //initialize a session. 
     rowCount = Convert.ToInt32(Session["clicks"]); 

     rowCount++; 

     //In each button clic save the numbers into the session. 
     Session["clicks"] = rowCount; 


     //Create the textboxes and labels each time the button is clicked. 
     for (int i = 0; i < rowCount; i++) 
     { 

      TextBox TxtBoxU = new TextBox(); 
      TextBox TxtBoxE = new TextBox(); 

      Label lblU = new Label(); 
      Label lblE = new Label(); 

      TxtBoxU.ID = "TextBoxU" + i.ToString(); 
      TxtBoxE.ID = "TextBoxE" + i.ToString(); 

      lblU.ID = "LabelU" + i.ToString(); 
      lblE.ID = "LabelE" + i.ToString(); 


      lblU.Text = "User " + (i + 1).ToString() + " : "; 
      lblE.Text = "E-Mail : "; 

      //Add the labels and textboxes to the Panel. 
      Panel1.Controls.Add(lblU); 
      Panel1.Controls.Add(TxtBoxU); 

      Panel1.Controls.Add(lblE); 
      Panel1.Controls.Add(TxtBoxE); 


     } 
    } 
+2

什麼不行?他們沒有出現?什麼是問題? – 2014-12-06 18:56:15

+1

剛剛在我身邊試過了,它工作正常。雖然, – Sean 2014-12-06 18:59:45

+0

失去了國家。我想這是你的問題? – Sean 2014-12-06 19:20:45

回答

0

嘗試:

<asp:Button runat="server" Text="Button" /> 
<asp:PlaceHolder ID="Panel1" runat="server" EnableViewState="true"></asp:PlaceHolder> 

protected void Page_Init(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     //Remove the session when first time page loads. 
     Session.Remove("clicks"); 
     Response.Write(Session["clicks"]); 
    } 
    else { 
    //initialize a session. 
    int rowCount = Convert.ToInt32(Session["clicks"]); 

    rowCount++; 

    //In each button clic save the numbers into the session. 
    Session["clicks"] = rowCount; 

     Panel1.Controls.Clear(); 
    //Create the textboxes and labels each time the button is clicked. 
     for (int i = 1; i <= rowCount; i++) 
     { 
      TextBox TxtBoxU = new TextBox(); 
      TextBox TxtBoxE = new TextBox(); 

      Label lblU = new Label(); 
      Label lblE = new Label(); 

      TxtBoxU.ID = "TextBoxU" + i.ToString(); 
      TxtBoxE.ID = "TextBoxE" + i.ToString(); 

      lblU.ID = "LabelU" + i.ToString(); 
      lblE.ID = "LabelE" + i.ToString(); 


      lblU.Text = "User " + (i).ToString() + " : "; 
      lblE.Text = "E-Mail : "; 

      //Add the labels and textboxes to the Panel. 
      Panel1.Controls.Add(lblU); 
      Panel1.Controls.Add(TxtBoxU); 

      Panel1.Controls.Add(lblE); 
      Panel1.Controls.Add(TxtBoxE); 
     } 
    } 
}