c# - 带有子控件的 UserControl 传递到 UserControl 中的 Repeater

标签 c# asp.net user-controls repeater

我正在努力实现...

用户控件(MyRepeater)

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

页面

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

Page 对象中的 ItemTemplate 将用作 UserControl 中 Repeater 的 ItemTemplate。

这样做的原因是我们在整个应用程序中使用了许多中继器,每个中继器和一些相应的按钮执行一些类似的代码。我想将这些代码全部保存在一个地方,以便开发人员可以使用我们的自定义 Repeater 控件,而不必在每次需要时都在每个页面中创建代码。

过去,我通过在我的 Repeater 控件的 RenderContents 方法中创建一个 Repeater 控件,对 WebControl 进行了类似的操作,一切都如我所料。

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

目前我的 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; }
}

简而言之,它只是不起作用... Page ItemTemplate 的内容在整个 UserControl 之后呈现,而不是在中继器内呈现。

任何人都可以指出我可能出错的地方和/或指出我更好的方向吗?

最佳答案

您进行了错误的解析和坚持。

你想要

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

关于c# - 带有子控件的 UserControl 传递到 UserControl 中的 Repeater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1577972/

相关文章:

c# - 监控日志数据类架构

asp.net - 如何为我的 C# 代码设置增加 ASP.NET session 超时?

c# - 向用户控件添加滚动条

c# - 如何将现代标签链接 float 到左侧?

c# - 初始化字符串的格式不符合从索引 0 开始的规范

c# - 如果(transform.rotation.z < 90)不工作

c# - 字符串无法转换为 double

asp.net - 如何使用或完成意大利面条代码?

c# - 在c#中打印变量名

c# - 如果在 Page.Load 中动态添加,则 UserControl 不会出现在页面上