asp.netuser-controlswebformsasp.net-controlscreatechildcontrols

What is the correct ASP.NET Control event/method in which to add nested controls?


What is the correct event/method in the ASP.NET life cycle to dynamically add Child Controls ?

My objective is to ensure that all the input controls on a User Control have the correct associated Validator and Label controls, based on configuration from an external file.

It seems like the correct place should be either OnInit(EventArgs e) or CreateChildControls(). Both of them are behaving a little bit unexpected, and rather than try to debug each of them, I figured I'd first ask you guys which one (or other) to use.


Solution

  • Its OnInit, and you need to do it on first load and on post back.