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
Post a Comment