SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to Microsoft,The EnsureChildControls() method is called in order to make sure that child controls have been created.

I see many developers call EnsureChildControls() in CreateChildControls() or Render(). Also, I have figured out that most web parts or controls works fine even without explicitly calling EnsureChildControls().

Any thoughts on when and how calling EnsureChildControls() make sense?

share|improve this question

On CreateChildControls event all the controls specified are created and added to controls collection. When the page is being rendered for the first time the method generally occurs after the OnLoad() event. In case of postback, it is called before the OnLoad() event. We can make use of EnsureChildControls() - It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it.

share|improve this answer
I already mentioned in the question what EnsureChildControls() does. The question is when exactly you would need ensure CreateChildControls called or not. – Amit Kumawat Nov 24 '11 at 15:14

The answer is obvious when you know that you don't need to call it if you don't want to. The child controls will be created for so you automatically.

The time when you want to call it yourself, is when you want to ensure that the child controls are there. For example, in a Render method, if you were going to consume a control, you would want to ensure that the control was there first otherwise a NullReferenceException would be thrown. So you would call EnsureChildControls() first just to make sure. In reality the controls almost certainly exist already by render but there may be other methods where you want to do a similar thing for initialization or data binding.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.