2013-12-13 66 views
4

我有一個由10行組成的表,每行包含3個字段(SKU,開始日期,結束日期)。我的目標是遍歷表格行並提取這些值。到目前爲止,我還沒有能夠提出一個可行的解決方案。下面是我到目前爲止有:C#/ ASP.NET遍歷表

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// 
} 

代碼,表格佈局:

<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> 
+0

什麼是當前代碼的結果? –

+0

它根本什麼都不做。 – user1698144

+0

細胞內有什麼?文本框? – Leo

回答

3

做到這一點,而不是....

  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); 

         } 
        } 
       } 
      } 

你應該找到行內的控制'cells'control collection instead of the rows'control collection