【问题标题】:UserControl with Child Controls passed into a Repeater within the UserControl带有子控件的 UserControl 被传递到 UserControl 中的 Repeater
【发布时间】:2009-10-16 13:12:21
【问题描述】:

我正在努力实现...

用户控件(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 之后呈现,而不是在转发器内。

谁能指出我可能出错的地方和/或指出更好的方向?

【问题讨论】:

  • 可能与生命周期有关吗?控件中的 Page_Init 将在 Page 的 Page_Init 之前运行 - 是否设置了 myRepeater.ItemTemplate?
  • 不是,我刚刚发现了那个确切的问题。 Page_Init 中的 this.ItemTemplate 为空。事实上,它似乎总是为空。我不确定我可以在哪里挑选它以便使用它,因为它还没有被渲染。

标签: c# asp.net user-controls repeater


【解决方案1】:

你得到了解析并坚持错误的方式。

你想要的

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

【讨论】:

  • 真是个笨蛋……是的……就是这样。谢谢!
猜你喜欢
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多