UserControl with child controls is passed to the repeater in UserControl

I'm trying to achieve something like ...

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 will be used as the Repeater ItemTemplate in the UserControl.

The reason for this is that we use several repeaters in our application, each repeater and some corresponding buttons execute some similar code. I would like to keep this code in one place so that developers can use our custom Repeater control, and not create code on each page every time it is necessary.

In the past, I did this with WebControl, creating a Repeater element in the RenderContents method of my repeater, and everything works as I expected.

The reason I would like to use UserControl, since there will be a number of Style / Layout type changes that may be required between systems, it will be much easier than UserControl, where html / asp can be edited directly.

Currently, the code of my UserControl is as follows.

 [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 just doesn't work ... The contents of the ItemTemplate page are displayed after the entire UserControl, and not inside the relay.

Can someone point out where I can go wrong and / or point me in a better direction?

+4
source share
1 answer

You have parsing and persistence is out of order.

Do you want to

 [ParseChildren(true)] [PersistChildren(false)] 
+4
source

All Articles