[英]How can i add HTML input from C#, ASP.net
I have this HTML table in my aspx page which will be populated from datatable, and I need to put a checkbox at the start of each row.我的 aspx 页面中有这个 HTML 表,它将从数据表中填充,我需要在每一行的开头放置一个复选框。 I tried to use the code below in which I used
StringBuilder
and I also tried using TagBuilder
but nothing works.我尝试使用下面使用
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()
});
Here is the Table :这是表:
<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>
Here are the results:结果如下:
I also want to be able to access the value of the checkbox (checked or not).我还希望能够访问复选框的值(选中或不选中)。
I am not sure about that but I think you can use something like this :我不确定,但我认为你可以使用这样的东西:
html.Append("<input type=\"checkbox\" name=\"CheckSelect").Append(row["id"]).Append("\">");
Or If you don't mind to use Jquery, here is a solution for you :或者,如果您不介意使用 Jquery,这里有一个适合您的解决方案:
https://forums.asp.net/t/2156602.aspx?Appending+the+Checkbox+to+the+HTML+Table+ https://forums.asp.net/t/2156602.aspx?Appending+the+Checkbox+to+the+HTML+Table+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.