Search code examples
asp.nettemplatesweb-user-controls

Can I nest Templates in a Web User Control?


I want to do something like this:

<MyTemplate>
    <span><%# Container.Title %></span>
    <MySubTemplate>
       <span><%# Container.Username %></span>
    </MySubTemplate>
</MyTemplate>

Assuming I have a list of Titles that each have a list of Usernames.. If this is a correct approach how can I do this or what is a better way?


Solution

  • If you have a list of titles, that each have their own list of UserNames, it seems like you want to do something with nested repeaters (or other controls), not templates...

        <asp:Repeater ID="rptTitle" runat="server" >
            <ItemTemplate>
                <%# Eval("Title") %>
                <asp:Repeater ID="rptUsers" runat="server" >
                    <ItemTemplate>
                        <%# Eval("UserName") %>
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>
    

    And then bind the rptUsers during the ItemDataBound event of rptTitle...