How can I create a dynamic button click event on a dynamic button?

AB Vyas picture AB Vyas · May 31, 2011 · Viewed 201.9k times · Source

I am creating one button on a page dynamically. Now I want to use the button click event on that button.

How can I do this in C# ASP.NET?

Answer

abatishchev picture abatishchev · May 31, 2011
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);

//protected void button_Click (object sender, EventArgs e) { }