.Net-在运行时将ASP按钮动态添加到ASPX中的表

时间:2018-08-01 04:58:24

标签: asp.net html-table

我需要在运行时在aspx表中添加按钮

table.Append("<table border='1' class='table table-striped' width='450px'>");
table.Append("<tr><th>Facilities</th><th>Size</th><th>InterestRate</th><th>View in Doc</th>");
table.Append("</tr>");

if (dr.HasRows)
{
    while (dr.Read())
    {
        table.Append("<tr >");
        table.Append("<td>" + dr[0] + "</td>");
        table.Append("<td>" + dr[1] + "</td>");
        table.Append("<td>" + dr[2] + "</td>");
        table.Append("<td>" + <asp:Button ID = 'btnOpenDoc' runat = 'server' CssClass = 'button rounded' Width = '200px' Text = 'Open Document' OnClick = 'RunPowerShellScript_OnClick()'/> + "</td>");
        table.Append("</tr>");
    }

}

我在此行上遇到问题:

table.Append("<td>" + <asp:Button ID = 'btnOpenDoc' runat = 'server' CssClass = 'button rounded' Width = '200px' Text = 'Open Document' OnClick = 'RunPowerShellScript_OnClick()'/> + "</td>");

1 个答案:

答案 0 :(得分:2)

您不能通过串联字符串来添加服务器控件。

尝试一下

伪代码

<form id="form1" runat="server">
<div>
    <asp:Table ID="table" runat="server">
        <asp:TableRow>
            <asp:TableCell>

            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>    
</div>
</form>

隐藏代码

protected void Page_Load(object sender, EventArgs e)
{
        Button btn = new Button()
        {
            ID = "btnOpenDoc",
            CssClass = "button rounded",
            Width = 200,
            Text = "Open Document"
        };
        table.Rows[0].Cells[0].Controls.Add(btn);
        btn.Click += RunPowerShellScript_OnClick;
}

protected void RunPowerShellScript_OnClick(object sender, EventArgs e)
{

}