title: C#/ASP.NET Iterate Through Table
body: <p>I have a table consisting of 10 rows, each row containing 3 fields(SKU, Start Date, End Date). My objective is to iterate through the table rows and extract these values. So far, I have not been able to come up with a working solution. Below is what I have so far:</p>
  
  <pre><code>protected void btnVerify_Click(object sender, EventArgs e)
  {
    //START LOOP THROUGH TABLE ROWS//
    foreach (TableRow row in Table1.Rows)
    {
      foreach (Control ctrl in row.Controls)
      {
        //CONTROL IS TEXBOXT: EXTRACT VALUES//
        if (ctrl is TextBox)
        {
           TextBox txt = (TextBox)ctrl;
           Label lbl = new Label();
           lbl.Text = txt.Text;
           PlaceHolder1.Controls.Add(lbl);            
        }    
      }                  
    }
    //END LOOP THROUGH TABLE ROWS//
  }
  </code></pre>
  
  <p>CODE FOR THE TABLE LAYOUT:</p>
  
  <pre><code>&lt;asp:Table id="Table1" runat="server" 
          CellPadding="3" 
          CellSpacing="0"
          GridLines="both"
          Caption="Sample Table" Width="640px"&gt;
  
          &lt;asp:TableHeaderRow id="Table1HeaderRow" 
              BackColor="GradientActiveCaption"
              runat="server"&gt;
              &lt;asp:TableHeaderCell 
                  Scope="Column" 
                  Text="Product SKU" /&gt;
              &lt;asp:TableHeaderCell  
                  Scope="column" 
                  Text="Start Day/Time" /&gt;
              &lt;asp:TableHeaderCell 
                  Scope="Column" 
                  Text="End Day/Time" /&gt;
          &lt;/asp:TableHeaderRow&gt;              
  
          &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU1" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart1" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd1" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU2" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart2" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd2" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU3" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart3" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd3" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU4" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart4" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd4" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU5" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart5" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd5" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU6" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart6" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd6" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU7" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart7" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd7" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU8" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart8" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd8" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU9" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart9" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd9" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
                   &lt;asp:TableRow&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtSKU10" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtStart10" CssClass="inpDate" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
              &lt;asp:TableCell&gt;&lt;asp:TextBox ID="txtEnd10" CssClass="inpDate" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;/asp:TableCell&gt;
          &lt;/asp:TableRow&gt;
  
  
          &lt;asp:TableFooterRow ID="TableFooterRow1" runat="server"
              BackColor="LightBlue"&gt;
  
          &lt;/asp:TableFooterRow&gt;                               
      &lt;/asp:Table&gt;
  </code></pre>