C#/ASP.NET 遍历表

标签 c# asp.net loops

我有一个包含 10 行的表格,每行包含 3 个字段(SKU、开始日期、结束日期)。我的目标是遍历表行并提取这些值。到目前为止,我还没有想出一个可行的解决方案。以下是我目前所拥有的:

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>

最佳答案

改为这样做....

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

                         }
                    }
                }
            }

您应该在行的单元格控件集合而不是行的控件集合中找到控件

关于C#/ASP.NET 遍历表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20560112/

相关文章:

javascript - 如何在 AJAX 调用中存储下拉值?

asp.net - 基础设施 WebGrid 与 Telerik Grid

c# - HTTP 处理程序问题

algorithm - 下面给出的代码超出了时间限制

更新多个表行内容时的 JavaScript 加载器

c# - 尽管将 ContentPlaceHolder 和 <div> 设置为宽度 :100%,但内容超出了屏幕

c# - 在 Gtk# 中,如何重置使用 GLib.Timeout.Add 设置的计时器?

c# - 依赖倒置原则 : High Level and Low Level module example

java - 包含信用卡过期年份的字符串数组,从当前年份算起再算 10 年

c# - 每当请求该类的任何方法时自动调用公共(public)方法