用C#遍历html表中的行 [英] Iterate through rows in an html table with C#
问题描述
我在一个aspx页面(C#)中有一个html表格,它具有像
I have an html table in an aspx page (C#) that has columns like
1.CheckBox 2.Text 3.Text 4.TextBox
我想一次遍历表中的一行并处理(运行一个基于column2的存储过程,基于复选框是否被选中。如何才能做到这一点?
I want to iterate through the table one row at a time and process (run a stored procedure based on column2) based on whether the checkbox is checked or not. How will I be able to do that?
推荐答案
假设您使用Table服务器控件,那么它就是:
Assuming you're using the Table server control then it's just:
foreach (TableRow row in table.Rows)
{
var checkBox = (CheckBox)row.Cells[0].Controls[0]; //Assuming the first control of the first cell is always a CheckBox.
if (checkBox.Checked)
{
var col2 = (TextBox)row.Cells[1].Controls[0];
/* Do Stuff With col2 */
}
else
{
/* Do Stuff */
}
}
如果您只使用普通的html表格(runat =server)作为html表单控件,然后将TableRow更改为HtmlTableRow,将CheckBox更改为HtmlInputCheckBox,将TextBox更改为HtmlInputText。所有这些控件都位于System.Web.UI.HtmlControls命名空间中。
If you're using just a regular html table (with runat="server") as well as html form controls then just change TableRow to HtmlTableRow, CheckBox to HtmlInputCheckBox, and TextBox to HtmlInputText. All of those controls are in the System.Web.UI.HtmlControls namespace.
这篇关于用C#遍历html表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!