-1

我正在尝试在更新面板中对网格视图进行排序。它编译,但我得到一个Object reverence not set to an instance of an object错误。我正在尝试遵循本指南,但在 vb 中

默认.aspx

 <ajx:UpdatePanel ID="ajaxpanel" runat="server">   
    <ContentTemplate>         
                <asp:GridView ID="gvProgramDetails" runat="server" AutoGenerateColumns="False" 
                    CssClass="gridview" DataKeyNames="ProgramNumber" 
             AllowPaging="True" PageSize="2" AllowSorting="True" OnSorting="gvProgramDetails_Sorting">
                    <Columns>
                        <asp:CommandField ControlStyle-CssClass="button delete" ShowDeleteButton="True">
                            <ControlStyle CssClass="button delete" />
                        </asp:CommandField>
                        <asp:CommandField ControlStyle-CssClass="button save" ShowEditButton="True">
                            <ControlStyle CssClass="button save" />
                        </asp:CommandField>
                        <asp:BoundField DataField="ProgramNumber" HeaderText="ProgramNumber" 
                            InsertVisible="False" ReadOnly="True" SortExpression="ProgramNumber" />
                        <asp:BoundField DataField="ProgramName" HeaderText="ProgramName" 
                            SortExpression="ProgramName" />
                        <asp:BoundField DataField="ProgramStatus" HeaderText="ProgramStatus" 
                            SortExpression="ProgramStatus" />
                        <asp:BoundField DataField="RecordType" HeaderText="RecordType" 
                            SortExpression="RecordType" />
                        <asp:BoundField DataField="ProgramInformation" HeaderText="ProgramInformation" 
                            SortExpression="ProgramInformation" />
                    </Columns>
                </asp:GridView>
                </ContentTemplate>

默认.aspx.vb

Imports System.Web.Services
Partial Class processes_ProgramTrack_Default
Public Property SortOrder() As String
        Get
            If (ViewState("SortOrder").ToString = "desc") Then
                ViewState("SortOrder") = "asc;"
            Else
                ViewState("SortOrder") = "desc"
            End If
            Return ViewState("SortOrder").ToString()
        End Get
        Set(ByVal value As String)
            ViewState("SortOrder") = value
        End Set
    End Property
    Public Sub bindGridView(Optional ByVal sortExp As String = "", Optional ByVal sortDir As String = "")
        Dim connstr As String = ConfigurationManager.ConnectionStrings("WEBConnectionString").ConnectionString
        Dim conn As New SqlConnection(connstr)
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        Dim myDataView As New DataView()
        Dim mysqlCommand As New SqlCommand("SELECT tblPrgTrackPrograms.ProgramNumber, tblPrgTrackPrograms.ProgramName, tblPrgTrackPrograms.ProgramStatus, tblPrgTrackProgramDocumentation.RecordType, tblPrgTrackProgramDocumentation.ProgramInformation FROM tblPrgTrackPrograms INNER JOIN tblPrgTrackProgramDocumentation ON tblPrgTrackPrograms.ProgramNumber = tblPrgTrackProgramDocumentation.ProgramNumber")
        Dim myDataSet As New DataSet()
        Dim mySQLAdapter As New SqlDataAdapter(mysqlCommand)
        mySQLAdapter.SelectCommand.Connection = conn

        mySQLAdapter.Fill(myDataSet)
        myDataView = myDataSet.Tables(0).DefaultView
        If sortExp <> String.Empty Then

            myDataView.Sort = String.Format("{0}{1}", sortExp, sortDir)
        End If
        gvProgramDetails.DataSource = myDataView
        gvProgramDetails.DataBind()
    End Sub

    Protected Sub gvProgramDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvProgramDetails.Load
        bindGridView()
    End Sub
    Protected Sub gvProgramDetails_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvProgramDetails.PageIndexChanging
        gvProgramDetails.PageIndex = e.NewPageIndex
        bindGridView()
    End Sub

    Protected Sub gvProgramDetails_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvProgramDetails.RowDeleting

    End Sub

    Protected Sub gvProgramDetails_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvProgramDetails.Sorting
        SortOrder = ViewState("SortOrder").ToString
        bindGridView(e.SortExpression, SortOrder)
    End Sub

End Class

我收到错误消息作为弹出消息框。我认为正在发生的事情,SortOrder 没有被分配一个值。

4

1 回答 1

0

ViewState("SortOrder")在尝试调用 ToString 之前,您需要确保其中有一个值。否则,它将抛出该异常(如您所见),因为您无法ToString在 null 上调用该方法。

本质上,您需要将对该 ViewState 变量的引用包装在“ If (ViewState("SortOrder") IsNot Nothing Then”块中,并可能在“”部分提供特殊处理Else(如果需要)。

你的财产是这样的:

Public Property SortOrder() As String
    Get
        If (ViewState("SortOrder") IsNot Nothing Then
            If (ViewState("SortOrder").ToString = "desc") Then
                ViewState("SortOrder") = "asc;"
            Else
                ViewState("SortOrder") = "desc"
            End If
        End If
        Return ViewState("SortOrder").ToString()
    End Get
    Set(ByVal value As String)
        ViewState("SortOrder") = value
    End Set
End Property

然后为您的排序事件做同样的事情:

Protected Sub gvProgramDetails_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvProgramDetails.Sorting
    If (ViewState("SortOrder") IsNot Nothing Then
        SortOrder = ViewState("SortOrder").ToString
        bindGridView(e.SortExpression, SortOrder)
    End If
End Sub

注意:鉴于您迄今为止提供的信息,这是我对您问题的最佳猜测。如果您提供引发错误的行,我很乐意再看看。

于 2012-06-07T19:33:39.823 回答