views:

353

answers:

1

I'm trying to achieve something along the lines of...

UserControl (MyRepeater)

<p>Control Start</p>
    <asp:Repeater id="myRepeater" runat="server" />
<p>Control End</p>

Page

<p>Page Start</p>
<uc1:MyRepeater ID="MyRepeater1" runat="server">
    <ItemTemplate>
        <p>Page Item Template</p>
    </ItemTemplate>
</uc1:MyRepeater>
<p>Page End</p>

The ItemTemplate from the Page object will be used as the ItemTemplate of the Repeater within the UserControl.

The reason for this is that we use a number of repeaters throughout our application, each repeater and some corresponding buttons perform some similar code. I would like to keep this code all in one place so that developers can use our custom Repeater control rather than having to create the code in every page every time it is needed.

I have done similar in the past with a WebControl by creating a Repeater control within the RenderContents method of my Repeater control and everything works as I would expect.

The reason I would like to use a UserControl as there will be a number of Style/Layout type changes that may need to be made between systems, this will be much easier as a UserControl where the html/asp can be edited directly.

Currently the code behind of my UserControl looks like this.

[ParseChildren(false)]
[PersistChildren(true)]
public partial class MyRepeater : System.Web.UI.UserControl, INamingContainer
{
    protected void Page_Init(object sender, EventArgs e)
    {
        myRepeater.ItemTemplate = this.ItemTemplate;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //Temporary bind for testing
        myRepeater.DataSource = Enumerable.Range(1, 5);
        myRepeater.DataBind();
    }

    [DefaultValue("")]
    [Browsable(false)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(RepeaterItem))]
    public virtual ITemplate ItemTemplate { get; set; }
}

In short, it's just not working... The contents of the Page ItemTemplate are being rendered after the entire UserControl and not within the repeater.

Can anyone indicate where I may be going wrong and/or point me in a better direction?

+3  A: 

You've got the parse and persist the wrong way round.

You want

[ParseChildren(true)]
[PersistChildren(false)]
Vincent
What a numpty... yes... that was exactly it. Thanks!
Robin Day