找到另一个转发器内的转发器

时间:2012-02-07 21:18:15

标签: asp.net vb.net nested-repeater

好的,我的问题是我有三个中继器。在那个中继器中,我有另一个中继器,第二个中继器。中间有更多,但这不相关。 HTML下面是我的VB代码。我的问题是rptCrashPercentageAvg没有任何东西。 rptCrashStatsDisplay如何访问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

1 个答案:

答案 0 :(得分:2)

我会尽量确保你在正确的地方寻找它。它会在你的转发器中找到控件的标题,因为它不会在那里找到它,所以你第一次尝试使用它时就没什么了。

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

否则你可以尝试一种效率更低的方法:

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

编辑:此外,由于实际 是转发器,因此您不需要CType。