简体   繁体   中英

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.

        <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:

                <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.' control was null. It can't find the control inside the Repeater as it tries to bind it because it isn't being created.

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.

Any ideas greatly appreciated :)

Obviously having a bad day yesterday. The answer is to declare and not inside the Repeater.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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