views:

172

answers:

2

I'm thinking about converting a few usercontrols to use templates instead. One of these is my own UC which contains some controls, one of which is a repeater. Is it possible to specifcy a template for the second level usercontrol from the template for the first (which would be on the page)?

+1  A: 

Assuming I understand your question correctly, try something like this:

Page.aspx:

<%@ Page Language="C#" %>
<%@ Register src="UC.ascx" tagname="UC" tagprefix="uc1" %>

<uc1:UC ID="UC1" runat="server">
<RepeaterTemplate>
    <%# Eval("Name") %> <%# Eval("Age") %><br />
</RepeaterTemplate>
</uc1:UC>

UC.ascx:

<%@ Control Language="C#" ClassName="UC" %>

<script runat="server">
    class Person {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e) {
        repeater1.ItemTemplate = RepeaterTemplate;
        repeater1.DataSource = new Person[] {
            new Person { Name="Joe", Age=20},
            new Person { Name="Jack", Age=30},
        };
        repeater1.DataBind();
    }

    public ITemplate RepeaterTemplate { get; set; }
</script>

<asp:Repeater runat="server" ID="repeater1">
</asp:Repeater>

This basically passes on the template specified on the outer page to the repeater in the user control.

It may not be exactly your scenario, but hopefully that'll give you ideas.

David Ebbo
Looks like this could work. I'll hold off marking as the answer until the bountry deadline is closed. Thanks. :)
Echilon
A: 

I'm not sure I understand your question, but I do alot of multi-level repeaters with dynamic templates. I use UserControls with no code as a convenient place to put the template html.

In code behind (such as an ItemDataBound event from a parent repeater), I select the appropriate template and set it:

repeater.ItemTemplate = MyBase.LoadTemplate(templateControlName)
repeater.DataSource = dataSource
repeater.DataBind()
Keith Walton