user1698144
user1698144

Reputation: 764

C#/ASP.NET Iterate Through Table

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:

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 FOR THE TABLE LAYOUT:

<asp:Table id="Table1" runat="server" 
        CellPadding="3" 
        CellSpacing="0"
        GridLines="both"
        Caption="Sample Table" Width="640px">

        <asp:TableHeaderRow id="Table1HeaderRow" 
            BackColor="GradientActiveCaption"
            runat="server">
            <asp:TableHeaderCell 
                Scope="Column" 
                Text="Product SKU" />
            <asp:TableHeaderCell  
                Scope="column" 
                Text="Start Day/Time" />
            <asp:TableHeaderCell 
                Scope="Column" 
                Text="End Day/Time" />
        </asp:TableHeaderRow>              

        <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU1" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart1" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd1" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU2" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart2" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd2" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU3" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart3" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd3" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU4" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart4" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd4" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU5" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart5" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd5" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU6" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart6" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd6" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU7" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart7" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd7" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU8" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart8" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd8" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU9" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart9" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd9" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>
                 <asp:TableRow>
            <asp:TableCell><asp:TextBox ID="txtSKU10" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtStart10" CssClass="inpDate" runat="server" ClientIDMode="Static"></asp:TextBox></asp:TableCell>
            <asp:TableCell><asp:TextBox ID="txtEnd10" CssClass="inpDate" runat="server"></asp:TextBox></asp:TableCell>
        </asp:TableRow>


        <asp:TableFooterRow ID="TableFooterRow1" runat="server"
            BackColor="LightBlue">

        </asp:TableFooterRow>                               
    </asp:Table>

Upvotes: 4

Views: 11806

Answers (1)

Leo
Leo

Reputation: 14830

Do this instead....

           foreach (TableRow row in tbl.Rows)
            {
                foreach (Table cell in row.Cells)
                {
                    foreach (Control ctrl in cell.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);

                         }
                    }
                }
            }

You should find your controls inside the rows' cells' control collection instead of the rows' control collection

Upvotes: 4

Related Questions