GridView,分页和"未设置对象引用"错误的问题

时间:2020-03-06 14:36:25  来源:igfitidea点击:

我陷入了以下问题。我正在尝试实现一个基本的GridView分页结果集,该结果集连接到Oracle数据库。就其本身而言,GridView和分页的结果都可以正常工作。当我尝试将其放入我们正在使用的页面布局类中时,就会出现问题。

我们有ClassA,它是从Page继承的,是公司标准。然后,我有了ClassB,它继承自ClassA并且包括特定于应用程序的代码。 GridView所在的页面继承自ClassB。在其他页面上,这一切似乎都可以正常工作,但我不认为这是问题的根源,但我想我提到了它。

发生的情况是,第一次加载带有GridView的页面时,一切看起来都很正常。查询运行,并显示前10条记录,下面是用于分页的数字。当我单击" 2"或者任何其他页面时,出现以下消息:"死亡黄屏":"对象引用未设置为对象实例"。在该错误行中引用的对象是" Me",即Page对象(调试器中的ASP.pagename_aspx)。我认为失败的确切行没有那么重要,因为我改变了几条语句的顺序,而最早的一条就失败了。

我已经使用调试器进行了追溯,它看起来很正常,只是在第1页上它工作正常,而在第2页上却失败了。

我已经实现了PageIndexChanging事件(同样,如果我从ClassB中删除继承,它本身也可以工作。而且,如果我尝试直接从ClassA中继承(完全绕过ClassB),仍然会遇到问题。

有任何想法吗?谢谢。

解决方案

我遇到了一种类似的情况,即基础(在示例中为ClassA)具有设置用于处理所有分页和排序位的变量,而GridView连接到使用这些变量的事件。没有在页面中设置适当的基类变量会导致完全相同的错误。

过去我遇到类似的问题时,通常是一个数据绑定问题(在正确的时间未调用DataBind(),因此当它尝试查看下一页时,DataSource为null)。

我同意@DotNetDaddy,因为我们需要确保将数据源设置为回发,因为这几乎可以肯定是"有趣"的黄色屏幕死亡的原因。下面是一个非常简单的示例,它显示了.NET 2.0+中GridView的排序和分页

以下是使用我的vb代码此gridview正常工作所需的确切标记

<asp:GridView ID="gridSuppliers" EnableViewState="false" runat="server" OnPageIndexChanging="gridSuppliers_PageIndexChanging" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" CssClass="datatable" CellPadding="0" CellSpacing="0" BorderWidth="0" GridLines="None">...</asp:GridView>

接下来是带有基于集合的数据绑定所需的排序/分页实现的代码隐藏文件

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Implements ISupplierView

    Private presenter As SupplierPresenter

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        presenter = New SupplierPresenter(Me)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        presenter.OnViewLoad()
    End Sub

    Protected Sub gridSuppliers_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gridSuppliers.PageIndexChanging
        gridSuppliers.PageIndex = e.NewPageIndex
        presenter.PopulateSupplierList()
    End Sub

    Private Sub gridSuppliers_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles gridSuppliers.Sorting
        If DirectCast(ViewState("PreviousSortExpression"), String) = e.SortExpression Then
            If DirectCast(ViewState("PreviousSortDirection"), String) = "Ascending" Then
                e.SortDirection = System.Web.UI.WebControls.SortDirection.Descending
                ViewState("PreviousSortDirection") = "Descending"
            Else
                e.SortDirection = System.Web.UI.WebControls.SortDirection.Ascending
                ViewState("PreviousSortDirection") = "Ascending"
            End If
        Else
            e.SortDirection = System.Web.UI.WebControls.SortDirection.Ascending
            ViewState("PreviousSortDirection") = "Ascending"
        End If
        ViewState("PreviousSortExpression") = e.SortExpression

        Dim gv As GridView = DirectCast(sender, GridView)
        If e.SortExpression.Length > 0 Then
            For Each field As DataControlField In gv.Columns
                If field.SortExpression = e.SortExpression Then
                    ViewState("PreviousHeaderIndex") = gv.Columns.IndexOf(field)
                    Exit For
                End If
            Next
        End If
        presenter.PopulateSupplierList()
    End Sub

#Region "ISupplierView Properties"
    Private ReadOnly Property PageIsPostBack() As Boolean Implements ISupplierView.PageIsPostBack
        Get
            Return Page.IsPostBack
        End Get
    End Property

    Private ReadOnly Property SortExpression() As String Implements ISupplierView.SortExpression
        Get
            If ViewState("PreviousSortExpression") Is Nothing Then
                ViewState("PreviousSortExpression") = "CompanyName"
            End If
            Return DirectCast(ViewState("PreviousSortExpression"), String)
        End Get
    End Property

    Public ReadOnly Property SortDirection() As String Implements Library.ISupplierView.SortDirection
        Get
            If ViewState("PreviousSortDirection") Is Nothing Then
                ViewState("PreviousSortDirection") = "Ascending"
            End If
            Return DirectCast(ViewState("PreviousSortDirection"), String)
        End Get
    End Property

    Public Property Suppliers() As System.Collections.Generic.List(Of Library.Supplier) Implements Library.ISupplierView.Suppliers
        Get
            Return DirectCast(gridSuppliers.DataSource(), List(Of Supplier))
        End Get
        Set(ByVal value As System.Collections.Generic.List(Of Library.Supplier))
            gridSuppliers.DataSource = value
            gridSuppliers.DataBind()
        End Set
    End Property
#End Region

End Class

最后,在后台代码中使用了presenter类

Public Class SupplierPresenter
    Private mView As ISupplierView
    Private mSupplierService As ISupplierService

    Public Sub New(ByVal View As ISupplierView)
        Me.New(View, New SupplierService())
    End Sub

    Public Sub New(ByVal View As ISupplierView, ByVal SupplierService As ISupplierService)
        mView = View
        mSupplierService = SupplierService
    End Sub

    Public Sub OnViewLoad()
        If mView.PageIsPostBack = False Then
            PopulateSupplierList()
        End If
    End Sub

    Public Sub PopulateSupplierList()
        Try
            Dim SupplierList As List(Of Supplier) = mSupplierService.GetSuppliers()
            SupplierList.Sort(New GenericComparer(Of Supplier)(mView.SortExpression, mView.SortDirection))
            mView.Suppliers = SupplierList
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class

**对演示者中引用的通用集合进行排序所需的类

Imports System.Reflection
Imports System.Web.UI.WebControls

Public Class GenericComparer(Of T)
    Implements IComparer(Of T)

    Private mDirection As String
    Private mExpression As String

    Public Sub New(ByVal Expression As String, ByVal Direction As String)
        mExpression = Expression
        mDirection = Direction
    End Sub

    Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements System.Collections.Generic.IComparer(Of T).Compare
        Dim propertyInfo As PropertyInfo = GetType(T).GetProperty(mExpression)
        Dim obj1 As IComparable = DirectCast(propertyInfo.GetValue(x, Nothing), IComparable)
        Dim obj2 As IComparable = DirectCast(propertyInfo.GetValue(y, Nothing), IComparable)
        If mDirection = "Ascending" Then
            Return obj1.CompareTo(obj2)
        Else
            Return obj2.CompareTo(obj1)
        End If
    End Function
End Class

我丢失了我以前用来发布此问题的未注册登录信息。

无论如何,哈珀·谢尔比的答案被证明是正确的。在该基类中有一个未设置的变量(一个自定义对象,这是我们的公司标准),该变量导致了问题(并且没有有用的错误消息)。

如果管理员或者具有适当权限的人看到此问题,则可以将Harper的答案标记为该答案,然后将其关闭。感谢大家的帮助。