嵌套转发器错误:对象引用未设置为对象的实例

时间:2011-07-28 10:51:06

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

我正在使用一些我之前用于嵌套列表视图的代码,并尝试使其与嵌套的Repeater一起使用,但是我收到了错误。

System.NullReferenceException:未将对象引用设置为对象的实例。

的.aspx

    <asp:Repeater ID="reMainNav" runat="server">
    <HeaderTemplate><ul></HeaderTemplate>
    <ItemTemplate><li><%# DataBinder.Eval(Container.DataItem, "name")%>

        <asp:Repeater ID="reSubNav" runat="server">
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <%# DataBinder.Eval(Container.DataItem, "name")%>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>

    </li></ItemTemplate>
    <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

的.vb

    Protected Sub reMainNav_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reMainNav.ItemDataBound

    Dim rowView As System.Data.DataRowView
    rowView = CType(e.Item.DataItem, System.Data.DataRowView)

    'database connection from web.config file
    Dim synergySQL As SqlConnection = New SqlConnection()
    synergySQL.ConnectionString = ConfigurationManager.ConnectionStrings("connSynergy").ConnectionString()

    'check if personal section complete
    Dim cmdSubNav As SqlCommand = New SqlCommand()
    cmdSubNav.Connection = synergySQL
    cmdSubNav.CommandText = "SELECT * FROM [subNavigation] WHERE [parentId] = " & rowView("id") & " ORDER BY [orderNo]"
    cmdSubNav.CommandType = CommandType.Text
    'data adapter
    Dim daSubNav As SqlDataAdapter = New SqlDataAdapter
    daSubNav.SelectCommand = cmdSubNav
    'data set
    Dim dsSubNav As DataSet = New DataSet()
    daSubNav.Fill(dsSubNav, "SubNav")

    Dim iSchedule As Integer
    iSchedule = dsSubNav.Tables(0).Rows.Count

    Dim reSubNav As Repeater = CType(e.Item.FindControl("reSubNav"), Repeater)
    reSubNav.DataSource = dsSubNav
    reSubNav.DataBind()

    synergySQL.Close()

End Sub

我是否必须为转发器做一些不同的事情?

感谢您的帮助。

学家

1 个答案:

答案 0 :(得分:3)

第一次标题行,因此您无法获得内部转发器控件。如下所示,检查项目类型。

Protected Sub reMainNav_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reMainNav.ItemDataBound

 If e.Item.ItemType == ListItemType.Item OrElse e.Item.ItemType == ListItemType.AlternatingItem Then

    Dim rowView As System.Data.DataRowView
    rowView = CType(e.Item.DataItem, System.Data.DataRowView)

    'database connection from web.config file
    Dim synergySQL As SqlConnection = New SqlConnection()
    synergySQL.ConnectionString = ConfigurationManager.ConnectionStrings("connSynergy").ConnectionString()

    'check if personal section complete
    Dim cmdSubNav As SqlCommand = New SqlCommand()
    cmdSubNav.Connection = synergySQL
    cmdSubNav.CommandText = "SELECT * FROM [subNavigation] WHERE [parentId] = " & rowView("id") & " ORDER BY [orderNo]"
    cmdSubNav.CommandType = CommandType.Text
    'data adapter
    Dim daSubNav As SqlDataAdapter = New SqlDataAdapter
    daSubNav.SelectCommand = cmdSubNav
    'data set
    Dim dsSubNav As DataSet = New DataSet()
    daSubNav.Fill(dsSubNav, "SubNav")

    Dim iSchedule As Integer
    iSchedule = dsSubNav.Tables(0).Rows.Count

    Dim reSubNav As Repeater = CType(e.Item.FindControl("reSubNav"), Repeater)
    reSubNav.DataSource = dsSubNav
    reSubNav.DataBind()

    synergySQL.Close()

End If

End Sub