【发布时间】:2014-08-05 15:30:54
【问题描述】:
当你创建一个 Table 对象时,包括 TableHeaderRow 和 TableHeaderCell 控件。 TableHeaderRow 的默认值和 TableFooterRow 控件使控件呈现 thead、tbody 和 tfoot 元素
我一直在尝试让 thead 和 tfoot 标记渲染但没有成功。
我正在使用我发布的那个链接中提到的类,我只得到tbody 和一堆嵌套的tr 标签。我正在使用 VS2012 pro 和 .net 4.5。
我做错了什么?
public void BuildHTMLTable(DataTable dt)
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
Table tbl = new Table();
// create table header
TableHeaderRow thr = new TableHeaderRow();
foreach (DataColumn col in dt.Columns)
{
TableHeaderCell thc = new TableHeaderCell();
thc.Text = col.Caption;
thr.Cells.Add(thc);
}
tbl.Rows.AddAt(0, thr);
// write out each data row
foreach (DataRow row in dt.Rows)
{
TableRow tr = new TableRow();
foreach (var value in row.ItemArray)
{
TableCell td = new TableCell();
td.Text = value.ToString();
tr.Controls.Add(td);
}
tbl.Rows.Add(tr);
}
// Create table footer
TableFooterRow tfr = new TableFooterRow();
foreach (DataColumn col in dt.Columns)
{
TableCell th = new TableCell();
th.Text = col.Caption;
tfr.Cells.Add(th);
}
tbl.Rows.Add(tfr);
// Renter the table to html writer
tbl.RenderControl(w);
// copy html writer to a string variable
tableString = sw.ToString();
}
下面呈现的 HTML
【问题讨论】: