简体   繁体   English

带有子控件的UserControl传入UserControl中的Repeater

[英]UserControl with Child Controls passed into a Repeater within the UserControl

I'm trying to achieve something along the lines of... 我正在努力实现......

UserControl (MyRepeater) 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. Page对象中的ItemTemplate将用作UserControl中Repeater的ItemTemplate。

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. 我想将这些代码保存在一个地方,以便开发人员可以使用我们的自定义Repeater控件,而不必在每次需要时在每个页面中创建代码。

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. 我通过在Repeater控件的RenderContents方法中创建一个Repeater控件,在过去使用WebControl做了类似的事情,一切都按照我的预期工作。

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. 我想使用UserControl的原因是因为系统之间可能需要进行许多样式/布局类型的更改,这将更容易作为可以直接编辑html / asp的UserControl。

Currently the code behind of my UserControl looks like this. 目前我的UserControl背后的代码如下所示。

[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. 简而言之,它只是不起作用...页面ItemTemplate的内容在整个UserControl之后呈现,而不是在转发器中。

Can anyone indicate where I may be going wrong and/or point me in a better direction? 任何人都可以指出我可能出错的地方和/或指出我的方向更好吗?

You've got the parse and persist the wrong way round. 你已经解析并坚持错误的方式。

You want 你要

[ParseChildren(true)]
[PersistChildren(false)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM