使用 C# 遍历 html 表中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2015676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:41:45  来源:igfitidea点击:

Iterate through rows in an html table with C#

c#htmlasp.nethtml-table

提问by Jaelebi

I have an html table in an aspx page (C#) that has columns like

我在 aspx 页面 (C#) 中有一个 html 表,它有这样的列

 1.CheckBox  2.Text  3.Text  4.TextBox

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?

我想一次遍历表一行并根据是否选中复选框进行处理(运行基于 column2 的存储过程)。我将如何能够做到这一点?

采纳答案by Rodrick Chapman

Assuming you're using the Table server control then it's just:

假设您使用的是 Table 服务器控件,那么它只是:

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

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.

如果您只使用普通的 html 表(使用 runat="server")以及 html 表单控件,那么只需将 TableRow 更改为 HtmlTableRow,将 CheckBox 更改为 HtmlInputCheckBox,将 TextBox 更改为 HtmlInputText。所有这些控件都在 System.Web.UI.HtmlControls 命名空间中。

回答by Tony The Lion

Check out the HtmlTable class and its Rows property.

查看 HtmlTable 类及其 Rows 属性。

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable(VS.71).aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable(VS.71).aspx

I'm sure that can help you on the way!

我相信这可以帮助你!

回答by Yoenhofen

If you add a runat="server" attribute to the table tag in html, you should be able to see the table as a collection of Control objects and you should be able to iterate through.

如果在 html 中向 table 标记添加 runat="server" 属性,您应该能够将 table 视为 Control 对象的集合,并且应该能够遍历。

This seems like the worst possible way to achieve dynamic data, however. Perhaps with some background we could help you find a better way...

然而,这似乎是实现动态数据的最糟糕的方法。也许有了一些背景,我们可以帮助您找到更好的方法......

回答by Steve Wortham

I already had some VB.NET code handy that can do this. It just took a little tweaking. It could be ported over to C# easily.

我已经有一些可以做到这一点的 VB.NET 代码。只需要稍微调整一下。它可以很容易地移植到 C#。

Protected Sub Page_Load()
    FindCheckBoxes(MyTable)
End Sub

Protected Sub FindCheckBoxes(ByRef ParentControl As Control)
    For Each ctrl As Control In ParentControl.Controls
        If TypeOf ctrl Is CheckBox Then
            If DirectCast(ctrl, CheckBox).Checked Then
                ' do something
            Else
                ' do something else
            End If
        ElseIf ctrl.HasControls Then
            FindCheckBoxes(ctrl)
        End If
    Next
End Sub

This is flexible enough to find checkboxes inside of anything (not just a table). However, in your particular scenario you may prefer to use something like noblethrasher's answer.

这足够灵活,可以在任何东西(不仅仅是表格)中找到复选框。但是,在您的特定场景中,您可能更喜欢使用诸如 noblethrasher 之类的答案。

My answer is a recursive method of crawling through the tree, finding every single checkbox. But noblethrasher's is a simple, straightforward, and more efficient algorithm if you know which column to look for the checkbox and you know it's not buried inside additional containers.

我的答案是在树中爬行的递归方法,找到每个复选框。但是,如果您知道要查找复选框的列并且您知道它没有隐藏在其他容器中,那么 noblethrasher 是一种简单、直接且更有效的算法。