views:

190

answers:

2

For example, I have a custom control called SampleControl. To simplify things for this question, pretend I'm making just a Panel Control clone from scratch.

<CC:SampleControl ID="Sample1" runat="server">
   <asp:Label ID="Label1" runat="server" Text="Hi"></asp:Label>
   <asp:Button ID="Button1" runat="server" Text="Button1"></asp:Label>
</CC:SampleControl>

To Output:

<div id="Sample1">
   <span id=Label1>Hi</span>
   etc.....
</div>

Where the Code is:

public class SampleControl: WebControl
{
    .....Render Stuff Goes Here.....
}

The ASP controls in the middle aren't being registered by ASP.NET. When I did a Reflector on the Panel control, I couldn't find out how they got the middle controls to render. It doesn't look like they are using a Templated Control.

When I do it, I get this error:

Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Any Ideas?

+5  A: 

First, [ParseChildren(false)] attributes lets parser know that you want your control to treat nested content as child controls.

Then make sure that somewhere in Render the RenderChildren method is called. It's the default implementation of System.Web.UI.Control.Render(), so you can do base.Render(writer) in your overriden Render method.

Alexey Rusakov
A: 

Easy:

  1. take a photo of your children
  2. put the photo on your harddrive
  3. point the background-image-property to the photo

Congratulations! Your children are now appearing in a custom webcontrol.