如何从C#,ASP.net添加HTML输入 [英] How can i add HTML input from C#, ASP.net

查看:53
本文介绍了如何从C#,ASP.net添加HTML输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的aspx页中有此HTML表,将从数据表中填充该HTML表,并且需要在每行的开头放置一个复选框.我尝试使用下面的代码,其中使用了 StringBuilder ,还尝试了使用 TagBuilder ,但没有任何效果.

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. I tried to use the code below in which I used StringBuilder and I also tried using TagBuilder but nothing works.

//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>

以下是结果:

我还希望能够访问该复选框的值(是否选中).

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("\">");

或者,如果您不介意使用Jquery,这是为您提供的解决方案:

Or If you don't mind to use Jquery, here is a solution for you :

https://forums.asp.net/t/2156602.aspx?将+复选框+添加到+ HTML +表格+

这篇关于如何从C#,ASP.net添加HTML输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆