Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When "must" I use asp.net CreateChildControls()?

Tags:

asp.net

While it seems that the "right" way to make a server control is to construct all child controls inside CreateChildControls call. But since it's difficult to know when it will be called (which is the whole point as a perf optimzation), I see most of our devs construct in OnInit, or OnLoad. And this works 99% of the case.

Are there cases where we have to use CreateChildControls?

like image 787
Xerion Avatar asked Mar 01 '23 06:03

Xerion


1 Answers

You should ALWAYS construct your child controls in CreateChildControls. This is the proper time in the Lifecycle to initialize and add them to the control tree. One of the reasons for this is that many times the method EnsureChildContols is called, which then calls CreateChildControls if necessary. Best Practice, just do it.

like image 101
Muad'Dib Avatar answered Mar 13 '23 05:03

Muad'Dib