Search code examples
asp.netrepeater

how to bind inner repeater?


i have a repeater inside another repeater. now i want to bind the inner repeater. but i m getting error of "Object reference not set to an instance of an object.". my code is

Protected Sub rep_test_ItemDataBound(ByVal sender As Object, ByVal e As   System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rep_test.ItemDataBound
    Dim dt As New DataTable
    dt = obj.getdata()
    Dim innerRepeater As Repeater = DirectCast(e.Item.FindControl("innerRepeater"), Repeater)
    innerRepeater.DataSource = dt
    innerRepeater.DataBind()
End Sub

my source code is

<asp:Repeater ID="rep_test" runat="server">
        <ItemTemplate>
            <div id='h<%# DataBinder.Eval(Container, "ItemIndex") %>' class="header" onclick='ToggleDisplay(<%# DataBinder.Eval(Container, "ItemIndex") %>);'>
                <%#DataBinder.Eval(Container.DataItem, "ID")%>
            </div>
            <div id='d<%# DataBinder.Eval(Container, "ItemIndex") %>' class="details">
            <asp:Repeater ID="rep_hello" runat="server">
            <ItemTemplate>
                <%#DataBinder.Eval(Container.DataItem, "batchid")%><br />
                <%#DataBinder.Eval(Container.DataItem, "ts")%><br />
            </ItemTemplate>
            </asp:Repeater>
            <%--    <%#DataBinder.Eval(Container.DataItem, "batchid")%><br />
                <%#DataBinder.Eval(Container.DataItem, "ts")%><br />--%>
            </div>
        </ItemTemplate>
    </asp:Repeater>

Solution

  • If you have a header or footer in the parent repeater, your method might be executed for them too, and hence not finding the inner control.

    Try to check if the e.Item.ItemType is "only" either ListItemType.Item or ListItemType.AlternatingItem, and only execute your code in this case.

    Of course can't guarantee if this is the problem. Check also for confirming that repeater ID is correct, and verify that it's directly inside the item template of the parent repeater, not inside another server control inside the item (or else, you'll need to find the other control first and then find the repeater inside it).

    Also, ensure you are using rep_hello ID not innerRepeater.

    BTW< you can do this in the markup too...

    <asp:repeater runat="server" id="innerRepeater"
        DataSource='<%# Eval("PropertyInParentObject") %>'  >
        ....
        ....
    </asp:repeater>
    

    You can use Container.DataItem instead of Eval too (and cast it to the type of the object in parent repeater item).