0

I have several buttons being created dynamically in cells of a table that is also being created dynamically.

In the first column of each row, I adds the button like this:

Button btnIdentifier = new Button();

btnIdentifier.Text = uniqueID;
btnIdentifier.Click += (s, e) => 
{
    Button button = s as Button;

    GetResults(button.Text);
};

cell.Controls.Add(btnIdentifier);

But when I click on the button, the event does not fire. The table that was previously dynamically created (along with the buttons) get removed from the page.

I know it's because after it does a postback, the buttons no longer exist on the page. But I don't know how to remedy that.

How can I create the buttons in the Page_Load method if they need to be created dynamically and may be different every time the code is executed? (It's for this same reason I can't just create them on the page and set Visible=false).

Thanks.

user3636407
  • 103
  • 1
  • 10
  • have a look at [How to handle the click event of a dynamic button](https://stackoverflow.com/questions/6187944/how-can-i-create-a-dynamic-button-click-event-on-a-dynamic-button) it might help. – vikscool Sep 05 '18 at 04:43
  • 1
    You need to recreate the table and the buttons in it on every PostBack. – VDWWD Sep 05 '18 at 07:46
  • @VDWWD That seems awfully difficult? Do I need to create like a bunch of public variables to store all the table cells, values, buttons, etc.? Is there an easy way of doing it? – user3636407 Sep 05 '18 at 11:20

0 Answers0