asp.net - Property null after postback - Dynamically loaded control -
i'm aware question has been asked many times before suspect have unique scenario.
i'm loading child control (ascx) , setting property on control. works fine until postback property null.
herewith first class loads childcontrol :
protected override void createchildcontrols() { myusercontrol control = (myusercontrol)page.loadcontrol(_ascxpath); control.myproperty = base.myproperty controls.add(control); }
then, on child control i've got following code:
public partial class myusercontrol : usercontrol { public mytype myproperty { get; set; } protected void page_load(object sender, eventargs e) { //exception on next line because property null (only on postback) var somevalue = myproperty.subproperty;
ok. let me try explain it.
1. once page created, full page lifecycle
2. click on control create user control, , it
3. entering value control, , getting postback
4. on server side postback handled, can see viewstate actions appear page loaded.
1 of main purposes of viewstate handling control events, see if changed, or save states or else.
5. if on moment, when viewstate loaded control still not constructed, it's events , values ignored.
solution either make static control , hide it, either create before viewstate actions started.
Comments
Post a Comment