If I have a custom user control (say, MyCustomControl.ascx) which contains a number of other controls including a repeater, is there any way to expose that repeater’s templates as templates for MyCustomControl?
For example, I would want to be able to do this:
<uc:MyCustomControl ID="MyCustomControl1" runat="server">
<ChildRepeaterHeaderTemplate>
<!-- code goes here -->
</ChildRepeaterHeaderTemplate>
<ChildRepeaterItemTemplate>
<!-- code that accesses values on the repeater's DataItem goes here -->
</ChildRepeaterItemTemplate>
<ChildRepeaterFooterTemplate>
<!-- code goes here -->
</ChildRepeaterFooterTemplate>
</uc:MyCustomControl>
There could potentially be more than one repeater or other templated controls within the parent control, in which case I would like to be able to expose all of those templates for all of the controls in the same way.
My first thought was to simply expose the repeater’s templates as ITemplate properties on MyCustomControl, but that didn’t work since there’s no (obvious) way to access the repeater’s DataItem from the templates doing it that way.
Like Brian says, you need a custom control, but if you had that, you could expose the ItemTemplates.
You need to instantiate your child repeaters before the control that includes this one tries to assign them.
This sample creates two child repeaters inside the control and binds them to some dummy data.
And with a page that uses this control:
You get the following output: