1

I need to add button at run time in my table in 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>");
    }

}

I'm having issue with this line:

table.Append("<td>" + <asp:Button ID = 'btnOpenDoc' runat = 'server' CssClass = 'button rounded' Width = '200px' Text = 'Open Document' OnClick = 'RunPowerShellScript_OnClick()'/> + "</td>");
1
  • What is the type of your table variable? Note also you are creating duplicate IDs as well which is a VeryBadThing. Commented Aug 1, 2018 at 5:22

1 Answer 1

2

you can't add server controls by concatenating strings.

try this,

PSEUDO CODE

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

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

Codebehind

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)
{

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.