c#创建thead和tbody

Pra*_*gus 7 c# vb.net web-applications html-table

谁能告诉我如何在我的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);
}
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 20

这是一个创建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);
Run Code Online (Sandbox Code Playgroud)

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


Ree*_*sey 6

TableRow基本上是tbody.

要创建thead节,请使用TableHeaderRow类而不是TableRow类.

(如果你想实现的话,还有btw,TableFooterRowtfoot.