Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem. While I am adding an event handler from code behind to a button the event never gets fired. But when I add it from when creating the button tag it works perfectly and I am creating the button from code behind and I adding it to table.
XML
<form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="show table" OnClick="Button1_OnClick" />
        <table border="1">
            <thead>
                <tr>
                </tr>
            </thead>
            <tbody id="tbody" runat="server">

            </tbody>
        </table>
    </div>
</form>


protected void Button1_OnClick(object sender, EventArgs e)
{
  var row = new TableRow();
  var btnDownload = new Button { ID = "ID", Text = "Click Here" };
  btnDownload.Click += ClickEvent;
  var cell = new TableCell();
  cell.Controls.Add(btnDownload);
  row.Controls.Add(cell);
  tbody.Controls.Add(row);
}
protected void ClickEvent(object sender, EventArgs e)
{
  Debug.WriteLine(((Button)sender).Text);
}
Posted

1 solution

Take backup of your button event code... delete the button from design mode... add new button and double click it and paste the code... This will fix the issue...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900