多个<tbody>
元素是不可能的<asp:Table>
,该TableSection
属性定义了应该在其中呈现行的“部分”,但是控件本身每个只呈现一个元素TableRowSection
至于异常,你不需要嵌套TableRow
对象,试试这个:
TableRow tbody = new TableRow();
tbody.TableSection = TableRowSection.TableBody;
TableCell cell = new TableCell();
tbody.Attributes.Add("data-filter", "featured");
cell.Text = "Featured";
tbody.Cells.Add(cell);
Table1.Rows.Add(tbody);
这是输出:
<table id="MainContent_Table1">
<tbody>
<tr data-filter="featured">
<td>Featured</td>
</tr>
</tbody>
</table>
为了实现您所追求的,您可以尝试创建一个自定义控件,或者可能是最简单的解决方案,以HtmlGenericControl
这种方式使用和构建表格元素,这不是最优雅的,但它具有预期的结果:
var table = new HtmlGenericControl("table");
var tbody = new HtmlGenericControl("tbody");
var tr = new HtmlGenericControl("tr");
var td = new HtmlGenericControl("td");
tr.Attributes.Add("data-filter", "featured");
td.InnerText = "Featured";
tr.Controls.Add(td);
tbody.Controls.Add(tr);
table.Controls.Add(tbody);
tbody = new HtmlGenericControl("tbody");
tr = new HtmlGenericControl("tr");
td = new HtmlGenericControl("td");
tr.Attributes.Add("data-filter", "featured");
td.InnerText = "Featured";
tr.Controls.Add(td);
tbody.Controls.Add(tr);
table.Controls.Add(tbody);
Page.Controls.Add(table);
这是输出:
<table>
<tbody>
<tr data-filter="featured">
<td>Featured</td>
</tr>
</tbody>
<tbody>
<tr data-filter="featured">
<td>Featured</td>
</tr>
</tbody>
</table>