我有一个由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>
答案 0 :(得分:4)
这样做......
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);
}
}
}
}
您应该在行'单元'控件集合中找到您的控件,而不是行的控件集合