Friday, July 30, 2010

Making a custom container control in ASP.Net (without templates)

If you're anything like me, you've tried to make your own ASP.Net server control that had container functionality, but were greeted with this exception when parsing:

"Type 'YourControlHere' does not have a public property named 'SomeHtmlElement'."

Next you goggled all possible permutations of "asp.net server control tutorial" and "child controls". Most of the solutions seemed to be either, "call EnsureChildControls", or "Use a template". However, you already called "EnsureChildControls", and if the ASP Panel control doesn't use a template, why would you?

To fix this inconvenience simply put the following attribute before your control class:
[ParseChildren(false)]

Now you are free to go about your business calling this.RenderChildren where ever and when ever you please!

No comments:

Post a Comment