【发布时间】:2020-03-06 11:09:05
【问题描述】:
我的 aspx 页面中有这个 HTML 表格,该表格将从数据表中填充,我需要在每一行的开头放置一个复选框。
我尝试使用下面的代码,其中我使用了StringBuilder,我也尝试使用TagBuilder,但没有任何效果。
//Building an HTML string.
StringBuilder html = new StringBuilder();
//Building the Data rows.
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
html.Append("<td>");
html.AppendFormat("< input type = 'checkbox' />"); //here it displays the tag as string
html.Append("</td>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<td>");
html.Append(row[column.ColumnName]); //here it displays the data
html.Append("</td>");
}
html.Append("</tr>");
}
datatableBody.Controls.Add(new Literal
{
Text = html.ToString()
});
这是表格:
<table class="table dataTable my-0" id="dataTable" style="text-align:center">
<thead>
<tr>
<th><input type="checkbox" /></th>
<th>Matricule</th>
<th>Prenom</th>
<th>Date</th>
<th>Periode</th>
<th>Total</th>
</tr>
</thead>
<tbody id="datatableBody" runat="server">
</tbody>
</table>
结果如下:
我还希望能够访问复选框的值(选中与否)。
【问题讨论】:
-
您是否尝试过使用
@Html.Raw()? -
将 html.AppendFormat 替换为 html.Append
-
@MarwenJaffel 试过了,没有任何变化。
-
@JamesS 你能提供更多细节吗?我搜索了一下,没有结果。
-
你可以使用HtmlTextWriter来添加html