title: How can I dynamically add rows to a table by pressing a button?
body: <p>I want to add rows to a table control by clicking a button. I tried using the code below but getting error, can anyone please help? </p>
  
  <pre><code>protected void btn_Click(object sender, EventArgs e)
  {
      //Table table = (Table)Page.FindControl("tblName");
      TableRow tr = new TableRow();
      tr.Cells.Add(new TableCell());
      tr.Cells.Add(new TableCell());
      tr.Cells.Add(new TableCell());
      tr.Cells[0].Text = TextBox1.Text;
      tr.Cells[1].Text = TextBox2.Text;
      tr.Cells[2].Text = TextBox3.Text;
      //add the row to the existing table.
      this.tblName.Rows.Add(tr);
      //this.tblName.Rows.Add();
  }
  </code></pre>
  
  <p>--------------------asp.net-------------------------</p>
  
  <pre><code>  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
      &lt;asp:Button ID="Button1" runat="server" Text="Button" onclick="btn_Click" /&gt;
      &lt;table id="tblName" Runat="server" style="width:100%;"&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
              &lt;td&gt;
                 &lt;asp:TextBox ID="TextBox2" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
              &lt;td&gt;
                 &lt;asp:TextBox ID="TextBox3" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
      &lt;/table&gt;
      &lt;br /&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>