title: Dynamically add table rows in asp table in asp.net
body: <p>I am trying to add table rows in asp table but could not solve it.</p>
  
  <p>I am trying to add new row on button click but it's generating on button click but on post back it disappears. How can i bind asp table so that it'll remain in table.</p>
  
  <p>Here is my code-</p>
  
  <p><strong>Default.aspx</strong></p>
  
  <pre><code>&lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
      &lt;asp:Table ID="tblAdd" runat="server" BorderWidth="2"&gt;
          &lt;asp:TableHeaderRow&gt;
              &lt;asp:TableHeaderCell ColumnSpan="2"&gt;
                  Add Languages
              &lt;/asp:TableHeaderCell&gt;
          &lt;/asp:TableHeaderRow&gt;
          &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;
                  Name&lt;font color="red"&gt;*&lt;/font&gt;
              &lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;
                  &lt;asp:TextBox ID="txtName" runat="server" Width="200"&gt;&lt;/asp:TextBox&gt;
              &lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
          &lt;asp:TableRow&gt;
          &lt;asp:TableCell&gt;
          &lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                       ErrorMessage="Required" Display="Dynamic" ControlToValidate="txtName"
                       ForeColor="Red"&gt;
                  &lt;/asp:RequiredFieldValidator&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
          &lt;asp:TableRow&gt;
              &lt;asp:TableCell ColumnSpan="2"&gt;
                  &lt;hr /&gt;
              &lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
          &lt;asp:TableRow&gt;
              &lt;asp:TableCell Width="50"&gt;
                  &lt;asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" BackColor="#999966"  /&gt;
              &lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;
                  &lt;asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" BackColor="#999966"  CausesValidation="False" /&gt;
              &lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
      &lt;/asp:Table&gt;
  
      &lt;asp:Label ID="Label1" runat="server" Text="&lt;font color=red&gt;*&lt;/font&gt;Required Field" Font-Size="Small"&gt;&lt;/asp:Label&gt;
      &lt;div style="background-color:Aqua; width:500px; border-color:Red"&gt;
          &lt;asp:Label ID="Label2" runat="server" Font-Size="X-Large"&gt;&lt;/asp:Label&gt;
      &lt;/div&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;asp:Table ID="tblLanguages" runat="server" BorderWidth="2"&gt;
              &lt;asp:TableHeaderRow&gt;
                  &lt;asp:TableHeaderCell ColumnSpan="2"&gt;
                      Languages
                  &lt;/asp:TableHeaderCell&gt;
              &lt;/asp:TableHeaderRow&gt;
              &lt;asp:TableRow&gt;
                  &lt;asp:TableCell Width="20"&gt;
                      &lt;asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" BackColor="#999966" /&gt;
                  &lt;/asp:TableCell&gt;
                  &lt;asp:TableCell&gt;
                      &lt;asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" BackColor="#999966" /&gt;
                  &lt;/asp:TableCell&gt;
              &lt;/asp:TableRow&gt;
              &lt;asp:TableRow&gt;
                  &lt;asp:TableCell BackColor="#FFE4B5"&gt;
                      &lt;asp:CheckBox ID="checkbox1" runat="server" /&gt;
                  &lt;/asp:TableCell&gt;
                  &lt;asp:TableCell BackColor="#FFE4B5"&gt;
                      Name
                  &lt;/asp:TableCell&gt;
              &lt;/asp:TableRow&gt;   
          &lt;/asp:Table&gt; 
  &lt;/form&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Default.aspx.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  
  public partial class _Default : System.Web.UI.Page
  {
  
      protected void Page_Load(object sender, EventArgs e)
      {
              tblAdd.Visible = false;
              Label1.Visible = false;
      }
  
  
  
      private void BindTable()
      {
          List&lt;TableRow&gt; testlist = new List&lt;TableRow&gt;();
          foreach (var item in testlist)
          {
              TableRow NewRow1 = new TableRow();
              TableCell NewCell1 = new TableCell();
              CheckBox newCheckBox = new CheckBox();
  
              NewCell1.Controls.Add(newCheckBox);
              NewRow1.Cells.Add(NewCell1);
  
              TableCell NewCell2 = new TableCell();
  
              Label newLable1 = new Label();
              newLable1.Text = txtName.Text;
  
              NewCell1.Controls.Add(newLable1);
              NewRow1.Cells.Add(NewCell1);
              tblLanguages.Rows.Add(NewRow1);
          }
      }
  
      protected void btnAdd_Click(object sender, EventArgs e)
      {
          tblAdd.Visible = true;
          btnAdd.Visible = false;
          btnDelete.Visible = false;
          Label1.Visible = true;
          Label2.Visible = false;
  
          BindTable();
      }
  
      protected void btnDelete_Click(object sender, EventArgs e)
      {
  
      }
      protected void btnCancel_Click(object sender, EventArgs e)
      {
           tblAdd.Visible = false;
           btnAdd.Visible = true;
           btnDelete.Visible = true;
      }
      protected void btnSave_Click(object sender, EventArgs e)
      {
          if (Page.IsValid)
          {
              btnAdd.Visible = true;
              btnDelete.Visible = true;
              Label2.Visible = true;
              tblAdd.Visible = false;
              Label2.Text = "Successfully Added";
              add();
  
              BindTable();
          }
  
          txtName.Text = "";
      }
  
      public int add()
      {
          string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
          SqlConnection sqlConnection = new SqlConnection(strcon);
  
          SqlCommand command = new SqlCommand("hrm_AddLanguages2", sqlConnection);
          command.CommandType = CommandType.StoredProcedure;
          command.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
          command.Parameters.Add("@CreatedOn", SqlDbType.DateTime).Value = DateTime.Now;
          command.Parameters.Add("@UpdatedOn", SqlDbType.DateTime).Value = DateTime.Now;
          command.Parameters.Add("@CreatedBy", SqlDbType.BigInt).Value = 1;
          command.Parameters.Add("@UpdatedBy", SqlDbType.BigInt).Value = 1;
          command.Parameters.Add("@IsDeleted", SqlDbType.Bit).Value = 0;
          sqlConnection.Open();
          return command.ExecuteNonQuery();
      }
  }
  </code></pre>
  
  <p>Please help me.</p>