0

I've got a Button called ButtonCreator when you click on it a click event called ButtonCreator_OnClick is fired, inside the function I've created a button dynamically, now how do I create on click event for that button?

the code:

private void ButtonCreator_OnClick(object sender, EventArgs e)
{
Button button = new Button();
button.Click += (object s, EventArgs eb) => {};
}

now when I click on the button the page is post back and the control is gone, how do I make the function inside the event handler work?

Robert
  • 872
  • 1
  • 8
  • 24
Caspian
  • 1
  • 1
  • Are you using [`WebForms`](https://www.asp.net/web-forms)? – Givi Jul 23 '17 at 18:19
  • 2
    Possible duplicate of https://stackoverflow.com/questions/6187944/how-can-i-create-dynamic-button-click-event-on-dynamic-button – abhinav pratap Jul 23 '17 at 18:32
  • @Givi yes I'm using WebForms – Caspian Jul 23 '17 at 18:40
  • 2
    Possible duplicate of [How can i create dynamic button click event on dynamic button?](https://stackoverflow.com/questions/6187944/how-can-i-create-dynamic-button-click-event-on-dynamic-button) – Peter Bons Jul 23 '17 at 18:49
  • The button (or any other dynamic control) is lost on PostBack unless it is recreated. If it is not it will not execute the method. – VDWWD Jul 23 '17 at 19:08

0 Answers0