Reputation: 25
After checking and rechecking my code, I cannot understand why the btnBack_Click
event continues to fire after I've removed the event and registered another one in Page_Load
btnBack.Click -= new EventHandler(btnBack_Click);
btnBack.Click += new EventHandler(btnPreviewBack_Click);
Is it the postback? Is it because I haven't removed the OnClick
on the aspx?
Upvotes: 2
Views: 266
Reputation: 8759
We can't see exactly where in your Page Lifecycle you are changing the Click
event, however, remember that absolutely everything (as far as event handler changes) is "undone" once the lifecycle begins (although events that caused the postback will still get executed).
When a postback happens (whether async or not), all the event handlers are restored to their original configuration.
The best approach is to add your event handler changes to the Page_PreRender
event:
protected void Page_PreRender(Object sender, EventArgs e)
{
if (_MustChangeEventHandler)
{
btnBack.Click -= new EventHandler(btnBack_Click);
btnBack.Click += new EventHandler(btnPreviewBack_Click);
}
}
Upvotes: 3