2

我正在使用一些以前用于嵌套列表视图的代码,并尝试使其与嵌套的中继器一起使用,但出现错误。

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

我必须为中继器做一些不同的事情吗?

谢谢你的帮助。

J。

4

1 回答 1

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
于 2011-07-28T11:21:17.177 回答