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

Although it seems that the โ€œrightโ€ way to make a server control is to create all the child controls inside the CreateChildControls call. But since itโ€™s hard to understand when it will be called (which is the whole point as optimization), I see that most of our developers are built in OnInit or OnLoad. And it works 99% of the time.

Are there any cases where we should use CreateChildControls?

+4
source share
4 answers

You should ALWAYS create child controls in CreateChildControls. This is the right time in Lifecycle to initialize and add them to the control tree. One reason for this is that the EnsureChildContols method is called many times, which then calls CreateChildControls, if necessary. Best practice, just do it.

+4
source

Read Management Execution Life Cycle

The CreateChildControls method is called whenever an ASP.NET page structure is to create a control tree, and this method call is not limited to a specific phase of the control's life cycle. For example, CreateChildControls can be called when a page loads, when data is bound, or during rendering.

+2
source

Performance pending the creation of a child control will save some unnecessary processor time on the server. For example, if an exception occurs or the thread is interrupted before calling CreateChildControls (), the clock cycles necessary to create these controls are saved.

What do you think about the fact that creating controls in OnInit is more indicative than during CreateChildControls ()?

0
source

You will be able to create controls in Init or Load until you write a control that should recreate the controls.

I find it best to create controls in CreateChildControls and then use EnsureChildControls to control that they are created when you need them. This allows you to reset controls by setting ChildControlsCreated to false and updating them if necessary.

0
source

All Articles