asp.net - In WebForms, why does my anonymous event handler not get called when added after OnLoad? -


i have asp.net webforms page several buttons added programmatically this:

    private void addexportbutton(control control, action clickaction) {         linkbutton exportbutton = new linkbutton {             text = "export",             enableviewstate = false /*buttons recreated on each postback anyway.*/         };         exportbutton.click += (sender, e) => clickaction();         control.controls.add(exportbutton);     } 

now works, long addexportbutton() method called along path onload() or onpreload() method. not fire handler action however, when addexportbutton() called onloadcomplete() method.

i add/create buttons when event handler (coming dropdown) gets called. happens after onload(), break code.

why this, , how can use anonymous methods event handlers in case?

see nice cheat sheet asp.net page lifecycle léon andrianarivony more info order of page/control creation.

in page life cycle, internal raisepostbackevent method (which raises button's click event) occurs between onload , onloadcomplete. if wait until onloadcomplete add linkbutton , hook click event, event won't raised: it's late.

(the fact you're using anonymous method irrelevant.)

can add export button in .aspx set visible property false when don't want appear?


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 -