I have 2 buttons. The first button gets drawn from the code behind. The second button gets drawn when the first button is clicked. This works perfectly. When I add a event when the second button gets clicked this event won't be triggered. As you can see in the code below there is a btnTwo_click function. If I put a breakpoint on this function the program won't even break. Is there a way to trigger this event from the code behind (not using java script)?
Here is the code, I actually use this system in a table. But this simple code has the same problem in the end.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += new CommandEventHandler(btnOne_Click);
form1.Controls.Add(btn);
}
void btnOne_Click(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += new CommandEventHandler(btnTwo_Click);
form1.Controls.Add(btn);
}
void btnTwo_Click(object sender, EventArgs e)
{
Label lblTest = new Label(); //BREAKPOINT
form1.Controls.Add(lblTest);
}
}
只需将此行移至Page_Load
事件即可运行;)
btn.Command += new CommandEventHandler(btnTwo_Click);
You have to add the second button on the Page_Load or Page_Init method of Page life cycle:
eg
protected void Page_Load(object sender, EventArgs e)
{
ImageButton btn = new ImageButton();
btn.Command += btnOne_Click;
form1.Controls.Add(btn);
ImageButton btn2 = new ImageButton();
btn2.Command += btnTwo_Click;
btn2.Visible = false;
form1.Controls.Add(btn2);
}
void btnOne_Click(object sender, EventArgs e)
{
// Your second button
form1.Controls[2].Visible = true;
}
void btnTwo_Click(object sender, EventArgs e)
{
ImageButton btn2 = (ImageButton)sender;
// Do something
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.