In my program I used a dynamically button with below code:
Button button = new Button();
button.ID = counter.ToString();
button.Text = "ok";
button.Click += new EventHandler(this.ButtonClick);
list.Controls.Add(button);
And I have added click event's code like below:
private void ButtonClick(object sender, EventArgs e)
{
//..
}
There are several solutions:
Dynamically created button not firing Click event
I have read and apply them but I cannot solve the problem.
My dynamic button operation is placed in Page_Load. (I tried it in Page_Init and it wasn't fired again.)
In order to debug I use break point. I put it to the Page_Load, for the first time the page works and the program stops at the break point, and then I continue.. After I click the dynamic button, the code wasnot fired...
My Page_Load like below:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//..
case "button":
Button button = new Button();
button.ID = counter.ToString();
button.Text = "ok";
button.Click += new EventHandler(this.ButtonClick);
list.Controls.Add(button);
break;
}
}