【问题标题】:How can i add HTML input from C#, ASP.net如何从 C#、ASP.net 添加 HTML 输入
【发布时间】: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

标签: c# html asp.net


【解决方案1】:

我不确定,但我认为你可以使用这样的东西:

html.Append("<input type=\"checkbox\" name=\"CheckSelect").Append(row["id"]).Append("\">");

或者如果你不介意使用 Jquery,这里有一个适合你的解决方案:

https://forums.asp.net/t/2156602.aspx?Appending+the+Checkbox+to+the+HTML+Table+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    相关资源
    最近更新 更多