asp.net button event not firing -


i have problem. while adding event handler code behind button event never gets fired. when add when creating button tag works , creating button code behind , adding table.

<form id="form1" runat="server">     <div>                                <asp:button id="button1" runat="server" text="show table" onclick="button1_onclick" />         <table border="1">             <thead>                 <tr>                 </tr>             </thead>             <tbody id="tbody" runat="server">              </tbody>         </table>     </div> </form>   protected void button1_onclick(object sender, eventargs e) {   var row = new tablerow();    var btndownload = new button { id = "id", text = "click here" };   btndownload.click += clickevent;   var cell = new tablecell();   cell.controls.add(btndownload);   row.controls.add(cell);   tbody.controls.add(row); } protected void clickevent(object sender, eventargs e) {   debug.writeline(((button)sender).text); } 

the answer question lies in understanding asp.net page lifecycle. asp.net reconstructs server instance of page on postback. once server done processing , response sent client, server instance destroyed forever, , can recreated using combination of data contained in browser (view data / cookies etc) , remaining server data (session / cache).

you're wiring click event of dynamic button in button1_onclick event handler. when asp.net tries reconstruct page on next postback, not run event handler (as button1 wasn't clicked) event handler never wired up, explaining why never runs. need wire event handlers in or before page_load handler in order capture control event handlers.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -