开发者

ASP.NET - Nested Custom Templates

I'm thinking about converting a few usercontrols to use templat开发者_开发问答es 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)?


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.


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜