This question was asked by one of my (most excellent) students this week:
In an ASP.NET Web Part for SharePoint, when I override CreateChildControls(), do I need to invoke the base?
You will see many examples on the Internet of code that does invoke the base, and many counter examples. Some of these contradictory examples are on the Microsoft MSDN site!
I decided to take ILSpy to the System.Web.dll assembly and find out once and for all.
First of all, System.Web.UI.WebControls.WebParts.WebPart does not override CreateChildControls() itself.
So, we must take a look at it’s ancestor: Part.
Once again, there is no implementation for CreateChildControls() here, so we go further up the chain to System.Web.UI.WebControls.Panel:
Still no definition of CreateChildControls(). Further up, we get System.Web.UI.WebControls.WebControl.
But it is not until we get to System.Web.UI.Control that we have an implementation:
Hang on a moment! That’s empty!
// System.Web.UI.Control /// <summary>Called by the ASP.NET page framework to /// notify server controls that use composition-based /// implementation to create any child controls they contain /// in preparation for posting back or rendering.</summary> protected internal virtual void CreateChildControls() { }
So, the simple answer appears to be:
No, you don’t need to invoke base.ControlChildControls() in an ASP.NET WebPart”