Search code examples
asp.netvb.netnested-repeater

Find a repeater that is within another repeater


Ok so my issue is I have three repeaters. Within that repeater I have another repeater and a third one in the second. There is more in between but that's not relevant. Below the HTML is my VB code. My issue is that rptCrashPercentageAvg reutrns Nothing. How can rptCrashStatsDisplay access rptCrashPercentageAvg?

<asp:Repeater ID="rptCrashStatsDisplay" runat="server">
        <ItemTemplate>
            <asp:Repeater ID="rptCrashPercentage" runat="server">
                <ItemTemplate>
                    <tr class="statsRowA">
                        <td class="emphasis" style="padding-left: 20px">
                            <%# DataBinder.Eval(Container.DataItem,"CRASH_TYPE_DESC") %>:
                        </td>
                        <td style="padding-left: 5px">
                            <%--background-color: <%# Iif(DataBinder.Eval(Container.DataItem,"CRASH_TYPE_PERCENT")>DataBinder.Eval(Container.DataItem,"COUNT(*)"), "red", "null") %>"--%>
                            <%#String.Format("{0:N1}", DataBinder.Eval(Container.DataItem, "CRASH_TYPE_PERCENT"))%>
                            %
                        </td>
                        <asp:Repeater ID="rptCrashPercentageAvg" runat="server">
                            <ItemTemplate>
                                <td style="padding-left: 5px">
                                    <%#String.Format("{0:N1}", DataBinder.Eval(Container.DataItem, "AVG_VAL"))%>
                                    %
                                </td>
                            </ItemTemplate>
                        </asp:Repeater>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
       </ItemTemplate>
    </asp:Repeater>
Private Sub rptCrashStatsDisplay_ItemDataBound(ByVal sender As System.Object, _
ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCrashStatsDisplay.ItemDataBound
    Dim dv As DataRowView = CType(e.Item.DataItem, DataRowView)
    If Not IsNothing(dv) Then
        Dim rptCrashPercentage As Repeater = CType(e.Item.FindControl("rptCrashPercentage"), Repeater)
        Dim view As DataView = dv.CreateChildView("statRel1")
        If (view.Count > 0) Then
            rptCrashPercentage.DataSource = view
            rptCrashPercentage.DataBind()
        End If
        Dim rptCrashPercentageAvg As Repeater = CType(e.Item.FindControl("rptCrashPercentageAvg"), Repeater)
        Dim viewAvg As DataView = dv.CreateChildView("statRel2")
        If (viewAvg.Count > 0) Then
            rptCrashPercentageAvg.DataSource = viewAvg
            rptCrashPercentageAvg.DataBind()
        End If
    End If
End Sub

Solution

  • I would try making sure you're looking for it in the correct place. It'll look in your repeater's Header for the control and since it won't find it there, it'll be Nothing the first time you try and use it.

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim rptCrashPercentageAvg As Repeater = CType(e.Item.FindControl("rptCrashPercentageAvg"), Repeater)
        'Shouldn't be "nothing" here.
    End If
    

    Otherwise you could try a more inefficient method:

    Dim rptCrashPercentageAvg As Repeater = CType(e.Item.FindControl("rptCrashPercentageAvg"), Repeater)
    If rptCrashPercentageAvg IsNot Nothing Then
        Dim viewAvg As DataView = dv.CreateChildView("statRel2")
        If (viewAvg.Count > 0) Then
           rptCrashPercentageAvg.DataSource = viewAvg
           rptCrashPercentageAvg.DataBind()
        End If
    End If
    

    Edit: Also, since it actually is a repeater, you shouldn't need the CType.