title: Read all rows and cell values dynamic table asp.net
body: <p>I found the tutorial to create a dynamic table and add rows:</p>
  
  <p><a href="http://geekswithblogs.net/dotNETvinz/archive/2009/06/29/faq-dynamically-adding-rows-in-asp-table-on-button-click.aspx" rel="nofollow noreferrer">http://geekswithblogs.net/dotNETvinz/archive/2009/06/29/faq-dynamically-adding-rows-in-asp-table-on-button-click.aspx</a></p>
  
  <p>How can I read all rows in the table, and then the values ​​of the textbox in the cells? The values ​​are entered in a table in a database (SQL Server).
  Can I continue to use C# and asp.net or do I have to use Javascript?</p>
  
  <p>I hope someone will give me help.</p>
  
  <p>This is the code:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Dynamic Adding of Rows in ASP Table Demo&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add New Row" /&gt;
      &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>CODE BEHIND:</p>
  
  <pre><code>public partial class _Default1 : System.Web.UI.Page
  {
      //A global variable that will hold the current number of Rows
      //We set the values to 1 so that it will generate a default Row when the page loads
      private int numOfRows = 1;
  
      protected void Page_Load(object sender, EventArgs e)
      {
          //Generate the Rows on Initial Load
          if (!Page.IsPostBack)
          {
              GenerateTable(numOfRows);
          }
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          if (ViewState["RowsCount"] != null)
          {
              numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
              GenerateTable(numOfRows);
          }
      }
  
      private void SetPreviousData(int rowsCount, int colsCount)
      {
          Table table = (Table)Page.FindControl("Table1");
          if (table != null)
          {
              for (int i = 0; i &lt; rowsCount; i++)
              {
                  for (int j = 0; j &lt; colsCount; j++)
                  {
                      //Extracting the Dynamic Controls from the Table
                      TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
                      //Use Request objects for getting the previous data of the dynamic textbox
                      tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
                  }
              }
          }
      }
  
      private void GenerateTable(int rowsCount)
      {
  
          //Creat the Table and Add it to the Page
          Table table = new Table();
          table.ID = "Table1";
          Page.Form.Controls.Add(table);
  
          //The number of Columns to be generated
          const int colsCount = 3;//You can changed the value of 3 based on you requirements
  
          // Now iterate through the table and add your controls
  
          for (int i = 0; i &lt; rowsCount; i++)
          {
              TableRow row = new TableRow();
              for (int j = 0; j &lt; colsCount; j++)
              {
                  TableCell cell = new TableCell();
                  TextBox tb = new TextBox();
  
                  // Set a unique ID for each TextBox added
                  tb.ID = "TextBoxRow_" + i + "Col_" + j;
                  // Add the control to the TableCell
                  cell.Controls.Add(tb);
                  // Add the TableCell to the TableRow
                  row.Cells.Add(cell);
              }
  
              // And finally, add the TableRow to the Table
              table.Rows.Add(row);
          }
  
          //Set Previous Data on PostBacks
          SetPreviousData(rowsCount, colsCount);
  
          //Sore the current Rows Count in ViewState
          rowsCount++;
          ViewState["RowsCount"] = rowsCount;
      }
  }
  </code></pre>