问题的GridView,寻呼和"对象引用未设置"错误 [英] Problem with a gridview, paging and "object reference not set" error

查看:87
本文介绍了问题的GridView,寻呼和"对象引用未设置"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我卡具有以下问题。我想实现一个基本的GridView分页的结果集,它可以连接到Oracle数据库。就其本身而言,在GridView和分页的结果,做工精细。当我试图把它放在我们在工作中的页面布局类的问题就来了。

I'm stuck with the following problem. I'm trying to implement a basic GridView paged result set, which connects to an Oracle database. By itself, the GridView, and the paged results, work fine. The problem comes when I try to put it in page layout class that we have at work.

我们有ClassA的,从页面继承,是一个企业标准。然后,我有ClassB的,它从ClassA的继承和,其中包括应用程序特定的code。该页面在GridView是从ClassB的继承。这一切似乎在其他页面做工精细,我不认为这是问题的根源,但我想我会提到它。

We have ClassA, which inherits from Page, and is a corporate standard. Then I have ClassB, which inherits from ClassA and which includes application-specific code. The page that the GridView is on inherits from ClassB. This all seems to work fine in other pages, and I don't think it's the source of the problem, but I thought I'd mention it.

什么情况是,在第一时间与GridView控件加载网页,一切看起来正常。在查询运行和前10个记录显示,并与下面的传呼号码。当我点击2或其他任何网页,我得到的死亡黄色屏幕,出现以下消息:对象未设置为一个对象的实例。对象在错误行被称作是我,Page对象(ASP.pagename_aspx在调试器)。我不相信它失败的确切行是重要的,因为我已经关掉了一些语句周围的秩序,它只是对失败最早的之一。

What happens is that the first time the page with the GridView loads, everything looks normal. The query runs and the first 10 records are displayed, with the numbers for paging below. When I click on "2" or any of the other pages, I get the "yellow screen of death" with the following message: "Object reference not set to an instance of an object". The object being referred to in that error line is "Me", the Page object (ASP.pagename_aspx in the debugger). I don't believe that the exact line it fails on is that important, because I've switched the order of a few statements around and it just fails on the earliest one.

我与调试器跟踪通过和它看起来很正常,只是在第1页能正常工作,而第2页失败。

I've traced through with the debugger and it looks normal, only that on Page 1 it works fine, and Page 2 it fails.

我已经实现PageIndexChanging事件(再次,它的工作原理本身,如果我从ClassB的继承删除。另外,如果我尝试直接从ClassA的继承(绕过ClassB的全部),我仍然得到这个问题。

I have implemented the PageIndexChanging event (again, it works by itself if I remove inheritance from ClassB. Also, if I try inheriting directly from ClassA (bypassing ClassB entirely), I still get the problem.

任何想法?谢谢你。

推荐答案

我@DotNetDaddy在同意你需要确保你设置后回数据源,因为这是几乎可以肯定为好玩的原因死亡的黄色画面。下面的是一个非常简单的例子,显示排序和分页在.NET 2.0的GridView +

I agree with @DotNetDaddy in that you need to make sure you set the datasource on post-back, as this is almost certainly the reason for the "fun" yellow screen of death. The below is a very simple example that shows sorting and paging for a GridView in .NET 2.0+

以下是此GridView控件所需的确切标记为w / VB我的code正常工作

The below is the exact markup required for this gridview to work correctly w/ my vb code

<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>

接下来是code-隐藏文件瓦特/为一个集合基于数据绑定所需的排序/分页执行

Next is the code-behind file w/ the required sorting/paging implementation for a collection based databind

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

最后,在$ C $使用presenter C类隐藏

And finally, the presenter class used in the code-behind

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

**排序是在presenter引用泛型集合所需的类

**the class required to sort a generic collection that is referenced in the presenter

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

这篇关于问题的GridView,寻呼和&QUOT;对象引用未设置&QUOT;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆