Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper use of EnsureChildControls()?

Most often I use it when I am accessing a property of a composite control that depends on a child control. But I have also added it to OnInit of a control so I could make sure a hidden field was added correctly. Just a minute ago I called it in RenderControl because I was having an issue rendering a calendar extender and it fixed it. I am starting to get a little confused on when I need to and when I don't need to call EnsureChildControls and when I should call it. Any pointers are welcome. Thanks!

like image 907
Mike Avatar asked Sep 21 '10 14:09

Mike


1 Answers

EnsureChildControls triggers CreateChildControl if it’s not already triggered before. This has to be done only one-time in the page life cycle. I call it unconditionally in OnInit / Page_Init and nowhere else. This place has the advantage that the controls are created before ASP.NET loads the ViewState. If you use the ViewState or ControlState it is necessary to create the child controls that early.

like image 192
Dirk Brockhaus Avatar answered Oct 21 '22 05:10

Dirk Brockhaus