Dynamically added control event does not fire
4 answers
Make sure this button is also dynamically created on each Page_Load. I often make the mistake of putting similar code inside:
If Not Page.IsPostback()
...
End if
However, since the Page_Load fires before the button click handler, if you cannot create the button during the postback, then the button will not exist at the time of its event.
0
source to share
Putting the code in an earlier event won't solve the problem. Even if you try to put your code in Page_Load () won't solve the problem.
As per the page lifecycle, try overriding the OnLoad function and re-creating and re-connecting the dynamically generated controls with the same IDs.
protected override void OnLoad(EventArgs e)
{
Button bb = new Button() { ID = "myBtn" }
bb.Click += new EventHandler(bb_Click);
PlaceHolderQuestions.Controls.Add(bb);
base.OnLoad(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
0
source to share