how to bind inner repeater?

醉酒当歌 提交于 2019-12-21 20:23:34

问题


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>

回答1:


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).




回答2:


You are trying to find a repeater with the ID "innerRepeater". You should use "rep_hello" instead:

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("rep_hello"), Repeater)
    innerRepeater.DataSource = dt
    innerRepeater.DataBind()
End Sub



回答3:


the follwing links will help you the most efficient way binding nested repeater 3 levels deep

msdn library



来源:https://stackoverflow.com/questions/7334488/how-to-bind-inner-repeater

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!