【问题标题】:c# create thead and tbodyc# 创建thead和tbody
【发布时间】:2010-11-23 03:03:50
【问题描述】:

谁能告诉我如何在我的 C# 代码中动态创建 thead tbody 标签?

private void MakeTable()
{
    Table tb = new Table();
    TableRow tr = new TableRow();
    TableCell td = new TableCell();
    td.Text="hello world";
    tr.Cells.Add(td);
    tb.Rows.Add(tr);
}

谢谢

【问题讨论】:

  • 不知怎的,我把它读作“线程”,这完全没有意义。抱歉打扰了。
  • 一样! (15 个烦人的字符占用空间)

标签: c# vb.net web-applications html-table


【解决方案1】:

这里是创建 THead、TBody 和 TFooter 的示例代码。

您基本上可以始终使用 TableRow 对象,只需重置 TableSection 属性。

    Table table = new System.Web.UI.WebControls.Table();
    TableRow tableRow;
    TableCell tableCell;

    tableRow = new TableRow();
    tableRow.TableSection = TableRowSection.TableHeader;
    tableCell = new TableCell();
    tableCell.Text = "HEADER";
    tableRow.Cells.Add(tableCell);
    table.Rows.Add(tableRow);

    tableRow = new TableRow();
    tableRow.TableSection = TableRowSection.TableBody;
    tableCell = new TableCell();
    tableCell.Text = "BODY";
    tableRow.Cells.Add(tableCell);
    table.Rows.Add(tableRow);

    tableRow = new TableRow();
    tableRow.TableSection = TableRowSection.TableFooter;
    tableCell = new TableCell();
    tableCell.Text = "FOOTER";
    tableRow.Cells.Add(tableCell);
    table.Rows.Add(tableRow);

    plhTest.Controls.Add(table);

虽然我建议直接用 html 构建表格并附加到页面。

【讨论】:

  • 您建议的任何具体原因(“在直接 html 中构建表格并附加到页面”)?
  • 如果您必须动态生成它(例如,由于未知的列数或其他原因),我发现 TableHeader 不会呈现为 thead 除非添加了 TableFooter(页脚可以有没有单元格,但必须将其添加到行集合中)。 grrr。
  • 注意通过表格的 Rows 属性而不是控件属性添加 TableHeaderRow。通过 Controls 属性添加 有时 意味着 thead 元素不会呈现为 html。
【解决方案2】:

TableRow 基本上是tbody

要创建 thead 部分,请使用 TableHeaderRow 类而不是 TableRow 类。

(顺便说一句,如果你想实现tfoot,还有TableFooterRow

【讨论】:

    【解决方案3】:
    var row = new TableHeaderRow() { TableSection = TableRowSection.TableHeader };
    table.Rows.Add(row);
    

    应该做的伎俩

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 2019-04-06
      • 2012-03-04
      • 1970-01-01
      • 2013-11-16
      • 2011-03-20
      • 1970-01-01
      相关资源
      最近更新 更多