简体   繁体   English

中继器不创建子控件

[英]Repeater not creating child controls

I'm trying to track down why DataBind() does indeed fire data bound events on a Repeater but no child controls - those inside its ItemTemplate tag - are injected.我试图找出为什么 DataBind() 确实在中继器上触发数据绑定事件,但没有注入子控件 - ItemTemplate 标签内的那些。

        <asp:Repeater ID="rptMessages" runat="server">
            <asp:ItemTemplate>
                <VallisLive2:NotificationWidget ID="ctlNotification" runat="server"
                    NotificationID='<%# Eval("NotificationID") %>'
                    Title='<%# Eval("Title") %>'
                    Details='<%# Eval("Details") %>'
                    CreatedDateTime='<%# Eval("CreatedDateTime") %>'
                    Importance='<%# Eval("Importance") %>' />
            </asp:ItemTemplate>
        </asp:Repeater>

The result of the above is that while there's no error, nothing appears inside the Repeater.上面的结果是,虽然没有错误,但中继器内部什么也没有出现。

I can prove that the DataSource contains items and if I try doing this:我可以证明 DataSource 包含项目,如果我尝试这样做:

                <asp:Repeater ID="rptMessages" runat="server" OnItemDataBound="rptMessages_ItemDataBound">

The event fires and the data values are present but this confirms that the child control inside the repeater isn't being created because this known workaround for this issue, setting the values manually like so:事件触发并且数据值存在,但这证实了转发器内的子控件没有被创建,因为此问题的已知解决方法,手动设置值,如下所示:


        protected void rptMessages_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
                e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var n = e.Item.DataItem as Notification;

                var control = e.Item.FindControl("ctlNotification") as NotificationWidget;

fails with: System.NullReferenceException: 'Object reference not set to an instance of an object.'失败: System.NullReferenceException: '未将对象引用设置为对象的实例。' control was null.控制为空。 It can't find the control inside the Repeater as it tries to bind it because it isn't being created.它在尝试绑定它时无法在 Repeater 中找到该控件,因为它没有被创建。

So DataBind() works, it fires, it has valid source data, but the end result is that the Repeater's output is empty, and the above proves the Repeater does not create the child control.所以DataBind() 起作用了,它触发了,它有有效的源数据,但最终的结果是Repeater 的输出是空的,上面证明Repeater 没有创建子控件。

Any ideas greatly appreciated :)任何想法都非常感谢:)

Obviously having a bad day yesterday.显然昨天过得很糟糕。 The answer is to declare and not inside the Repeater.答案是声明而不是在Repeater里面。

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

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