C# HtmlTable、HtmlTableRow、HtmlTableCell - 创建 thead、tbody 和 tfoot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2052003/
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 23:02:06  来源:igfitidea点击:

HtmlTable, HtmlTableRow, HtmlTableCell - creating thead, tbody and tfoot

c#html

提问by Hyman

I'm working with c# ASP .Net, HtmlTable, HtmlTableRow, HtmlTableCell to create a table.

我正在使用 c# ASP .Net、HtmlTable、HtmlTableRow、HtmlTableCell 创建一个表。

for example... i need to have some cells to be wrapped by <tfoot> and </tfoot>

例如......我需要有一些细胞被包裹 <tfoot> and </tfoot>

i tried to do it with HtmlGenericControl to wrap these cells with the tag "tfoot" to the HtmlTable but it didn't work.

我尝试使用 HtmlGenericControl 将这些带有“tfoot”标签的单元格包装到 HtmlTable 中,但没有成功。

i know that HtmlTableCell can have in the constructor "th" for "thead" and "td" for tbody. but i need them to be wrapped with the actual "thead" and "tbody".

我知道 HtmlTableCell 可以在构造函数中包含“thead”的“th”和 tbody 的“td”。但我需要用实际的“thead”和“tbody”包裹它们。

Any Suggestions??

有什么建议??

采纳答案by JR Kincaid

Here is how (below). All classes used are in System.Web.UI.WebControls.

这是方法(如下)。所有使用的类都在 System.Web.UI.WebControls 中。

        TableRow headerRow = new TableHeaderRow();
        TableRow row2 = new TableRow();
        TableRow row3 = new TableFooterRow();
        Table table = new Table();

        var cell1 = new TableCell();
        headerRow.TableSection = TableRowSection.TableHeader;
        cell1.Text = "Header";
        headerRow.Cells.Add(cell1);

        var cell2 = new TableCell();
        cell2.Text = "Contents";
        row2.Cells.Add(cell2);

        var cell3 = new TableCell();
        cell3.Text = "Footer";
        row3.Cells.Add(cell3);
        row3.TableSection = TableRowSection.TableFooter;


        table.Rows.Add(headerRow);
        table.Rows.Add(row2);
        table.Rows.Add(row3);
        this.Controls.Add(table);

回答by graphicdivine

Doesn't look good, I'm afraid. This from http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable%28VS.80%29.aspx

恐怕不好看 这来自http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltable%28VS.80%29.aspx

Note

A complex table model is not supported. You cannot have an HtmlTable control with nested < caption >, < col >, < colgroup >, < tbody >, < thead >, or < tfoot > elements. These elements are removed without warning and do not appear in the output HTML. An exception will be thrown if you attempt to programmatically add these table model elements to the Control.Controls collection of the HtmlTable control.

笔记

不支持复杂的表模型。您不能拥有带有嵌套 <caption>、<col>、<colgroup>、<tbody>、<thead> 或 <tfoot> 元素的 HtmlTable 控件。这些元素会在没有警告的情况下被删除,并且不会出现在输出 HTML 中。如果您尝试以编程方式将这些表模型元素添加到 HtmlTable 控件的 Control.Controls 集合中,则会引发异常。

Old school html is the way, then.

老派的 html 就是这样。

回答by vernmic

graphicdivine has the correct response, that functionality is not available to build the table programatically using htmlcontrols. You could could build it in the markup with html tags and add runat server where needed or you build it programatically using the webcontrols. like Table, TableRow etc...

graphicsdivine 有正确的响应,该功能无法使用 htmlcontrols 以编程方式构建表格。您可以在带有 html 标签的标记中构建它,并在需要的地方添加 runat 服务器,或者您可以使用 webcontrols 以编程方式构建它。像表,TableRow 等...

回答by Ev?en ?erny

You can use

您可以使用

HtmlGenericControl table = new HtmlGenericControl("table");

instead of

代替

HtmlTable table = new HtmlTable();