<p>When loading a page for the first time (!IsPostback), I am creating a button in code and adding it to my page, then adding an event handler to the click event.</p>

<p>However, when clicking the button, after the page reloads, my event handler does not fire.</p>

<p>Can anyone explain why?</p>

[{'answer_id': 42559, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 2, 'selected': False, 'text': '<p>You need to add the button always not just for non-postbacks.</p>\n'}, {'answer_id': 42563, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 0, 'selected': False, 'text': '<p>That is because the event binding that happens needs to be translated in to HTML.  This postback that happens if bound to the page between OnInit and OnLoad.  So if you want the button to bind events correclty make sure you do the work in OnInit.</p>\n\n<p>See the Page Life Cycle explaination.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms178472.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms178472.aspx</a></p>\n'}, {'answer_id': 42570, 'author': 'Mark Struzinski', 'author_id': 1284, 'author_profile': 'https://Stackoverflow.com/users/1284', 'pm_score': 1, 'selected': False, 'text': '<p>If you are not reattaching the event handler on every postback, then the event will not exist for the button. You need top make sure the event handler is attached every time the page is refreshed. So, here is the order of events for your page:</p>\n\n<ol>\n<li>Page is created with button and event handler is attached</li>\n<li>Button is clicked, causing a postback</li>\n<li>On postback, the page_load event skips the attaching of the event handler becaue of your !IsPostback statement</li>\n<li>At this point, there is no event handler for the button, so clicking it will not fire your event</li>\n</ol>\n'}, {'answer_id': 42602, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': True, 'text': '<p>@Brad:  Your answer isn\'t complete; he\'s most likely doing it too late in the page lifecycle, during the Page_Load event.</p>\n\n<p>Okay, here\'s what you\'re missing.</p>\n\n<p>ASP.NET is stateless.  That means, after your page is rendered and sent to the browser, the page object and everything on it is destroyed.  There is no link that remains on the server between that page and what is on the user\'s browser.</p>\n\n<p>When the user clicks a button, that event is sent back to the server, along with other information, like the hidden viewstate field.  </p>\n\n<p>On the server side, ASP.NET determines what page handles the request, and rebuilds the page from scratch.  New instances of server controls are created and linked together according to the .aspx page.  Once it is reassembled, the postback data is evaluated.  The viewstate is used to populate controls, and events are fired.</p>\n\n<p>This all happens in a specific order, called the <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.page_events.aspx" rel="nofollow noreferrer">Page Lifecycle</a>.  In order to do more complex things in ASP.NET, such as creating dynamic controls and adding them to the web page at runtime, you MUST understand the page lifecycle.  </p>\n\n<p>With your issue, you must create that button every single time that page loads.  In addition, you must create that button BEFORE events are fired on the page.  Control events fire between Page_Load and Page_LoadComplete.</p>\n\n<p>You want your controls loaded before ViewState information is parsed and added to controls, and before control events fire, so you need to handle the PreInit event and add your button at that point.  Again, you must do this EVERY TIME the page is loaded.</p>\n\n<p>One last note; page event handling is a bit odd in ASP.NET because the events are autowired up.  Note the Load event handler is called Page_Load...</p>\n'}]
['https://Stackoverflow.com/questions/42554', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1583/']