Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Questions: 1. is this possible and how?</p> <p>Answer. Yes it is possible and here how can you do it.</p> <ol> <li><p>Start with 1 label , 1 text box and 2 buttons and 2 panel as </p> <pre><code>&lt;asp:Panel ID="Panel1" runat="server"&gt; &lt;asp:Label ID="Label1" runat="server" Text="Item1"&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;/asp:Panel&gt; &lt;asp:Panel ID="Panel2" runat="server"&gt; &lt;asp:Button ID="Button1" runat="server" Text="Add more Item" onclick="Button1_Click" /&gt; &lt;asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Submit" /&gt; &lt;/asp:Panel&gt; </code></pre> <p></p></li> </ol> <p>The back end code will be</p> <pre><code> protected void Page_Init(object sender, EventArgs e) { if (!Page.IsPostBack) { Session["cnt"]= null; } if (Session["cnt"]!=null) { int cnt = (int)Session["cnt"];//get how many items are already there and add them to page for (int i = 0; i &lt; cnt; i++) { TextBox tb = new TextBox(); tb.ID = "tb" + i.ToString(); Label lb = new Label(); lb.ID = "lb" + i.ToString(); lb.Text = "item " + (i + 2).ToString(); Panel1.Controls.Add(lb); Panel1.Controls.Add(tb); } } } protected void Button1_Click(object sender, EventArgs e) { int cnt = 0; if (Session["cnt"] != null) { cnt = (int)Session["cnt"]; } cnt++; TextBox tb = new TextBox(); tb.ID = "tb" + cnt.ToString(); Label lb = new Label(); lb.ID = "lb" + cnt.ToString(); lb.Text = "item " + (cnt + 2).ToString(); Panel1.Controls.Add(lb); Panel1.Controls.Add(tb); Session["cnt"] = cnt; } protected void Button2_Click(object sender, EventArgs e) { int cnt = 0; if (Session["cnt"] != null) { cnt = (int)Session["cnt"]; } for (int i = 0; i &lt; cnt; i++) { TextBox tb = (TextBox)Panel1.FindControl("tb" + i.ToString()); Label lb = (Label)Panel1.FindControl("lb" + i.ToString()); if (tb!=null&amp;&amp;lb!=null) { Response.Write(lb.Text + " - " + tb.Text + "&lt;br/&gt;"); } } } </code></pre> <p>question 2. is there any other appropriate way? Answer: It depends upon the context. You can do this same thing using javascript which will save the roundtrip to server and will be faster.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload