Tuesday, December 11, 2007

OnInit not firing on controls with default property

Just build some nested controls to run on the web site, but there was some strange stuff going on.
The render method worked fine, but there weren't some values on those controls. The cause of this was that the OnInit, and the OnLoad event's weren't firing.
After some debugging and analyzing the control tree in /trace.axd, I saw there were no controls in the main control.
The reason? I'd put a [ParseChildren(ChildrenAsProperties = true,
DefaultProperty = "Groups")]
attibute on the main control, and all those controls were added to a List collection, but not to the Control collection. So ASP.net didn't took up those child controls for inclusion in the control tree.

A dirty line in the main control OnInit fixed this:
foreach (Group group in Groups) { Controls.Add(group); }.

Share