I am writing a composite control that should render any content placed between it's opening and closing tag in the consuming aspx page.
VB
Public Class MyComposite
Inherits CompositeControl
Implements INamingContainer
Public Property UserContentTemplate as ITemplate = Nothing
Public Overrides ReadOnly Property Controls() As ControlCollection
Get
EnsureChildControls()
Return MyBase.Controls
End Get
End Property
Protected Overrides Sub CreateChildControls()
' This is where I'm creating the controls
' for the composite
End Sub
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
' This is where I render the composite controls
End Sub
Protected Overrides Sub RecreateChildControls()
EnsureChildControls()
End Sub
Public Overrides Sub DataBind()
CreateChildControls()
ChildControlsCreated = True
MyBase.DataBind()
End Sub
End Class
From here the UserContentTemplate is available in the consuming aspx page
<cc:MyComposite runat="server" ID="MyCompositeID">
<UserContentTemplate>
<asp:Button... />
<asp:TextBox... />
</UserContentTemplate>
</cc:MyComposite>
at this point, the asp:Button and asp:TextBox are not being rendered. I have checked out this link Building Templated Custom ASP.NET Serv Controls, but I don't know if this applies or how to apply it in my situation. If you look at the link, you'll see that there are HTML elements inside the <StatsTemplate> tag which are rendered in the custom control.
I found the answer here: Web Control Templates Explained written by Miguel Castro. The article helped clarify what was going on in the Microsoft article I referenced in the question.
Main Points
MyTemplate) that inherits fromCompositeControland implementsINamingContainerUserContentTemplatewithTemplateContainerattribute of typeMyTemplateUserContentTemplateinCreateChildControlsRenderoverrideDataBindoverride, I didn't need it in my caseThe
UserContentTemplateis instantiated as soon as theUserContentTemplatetag is used in the aspx page.The code in this answer solves the issue of being able to render consumer (developer) content in a composite control using Templates.