Some of the things to watch out for when overriding CreateChildControls:
- Instantiate a control as a first step, add a control to the Controls collection as a last one. The moment you add the control into the Controls collection, it will play what is called a “catch-up” where it flows through the lifecycle phases until it reaches the current phase of the page. Adding control to the Controls collection has also important implications when it comes to what is being considered an initial data (data you would normally put in your xml markup on the page or the user control). Any property values you assign before adding the control to the collection are not persisted in view state.
- Call base.CreateChildControl method, since you are overwriting the base functionality. This is not a mandatory step, but more often than not, you want to take advantage of the functionality that the child controls of the base class provide.
- Do not rely on IsPostBack when loading control with data in web parts. This is a documented issue, where by adding the web part to the page the postback occurs. From that point on, every subsequent request will be considered a postback.
- Take advantage of events in the child controls. This ensures proper flow of execution handled by the controls themselves. For instance, use Load event of the GridView control to load the data.
- Do not call CreateChildControls explicitly, rather rely on the EnsureChildControls() method to make sure controls have been properly loaded. This is especially true when exposing properties that delegate to the properties of the child control. You don’t have to worry about executing the same logic many times – EnsureChildControls checks the ChildControlsLoaded boolean flag, which is set by the CreateChildControls method, first time that method is called. If it hasn’t been called up to controls PreRender event, the PreRender will call CreateChildControls so that the controls are properly instantiated prior to the Render event.
Source: http://pfefferkorn.net/CS07/blogs/tomek/archive/2007/04/28/test-blog.aspx