2
我目前正在嘗試編寫代碼,以便在更改GridView上的頁面時保持排序首選項。我目前的代碼工作正常,基於用戶排序選擇對數據進行排序。但是當更改頁面時,排序不起作用。我沒有遇到錯誤,但沒有得到任何結果。維護GridView中的分頁排序
這裏是我的aspx代碼:
<asp:GridView ID="tblAdministrators" runat="server" AutoGenerateColumns="false" EmptyDataText="No records found" PageSize="75" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:BoundField HeaderText="Name" InsertVisible="False" DataField="FULLNAME" SortExpression="FULLNAME"></asp:BoundField>
</Columns>
<Columns>
<asp:BoundField HeaderText="Active" InsertVisible="False" DataField="ACTIVE" SortExpression="ACTIVE"></asp:BoundField>
</Columns>
</asp:GridView>
這裏是我的VB代碼:
Public Class ViewUsers
Inherits SolutionBasePage
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
tblAdministrators.DataSource = GetData()
tblAdministrators.DataBind()
End If
End Sub
Private Const ASCENDING As String = " ASC"
Private Const DESCENDING As String = " DESC"
Private Function GetData() As Data.DataView
Dim connection As OracleDBConnect = DAL.GetOracleDBConnection()
Dim request As OracleDBRequest = Nothing
Dim result As OracleDBResult = Nothing
Dim trace As OracleDBChronoTrace = Nothing
Dim status As DBStatus
Dim sb As New StringBuilder
Dim dv As DataView
Dim ds As Data.DataSet = New Data.DataSet
Try
With sb
.Append("SELECT FULLNAME, ACTIVE FROM USERS")
End With
request = New OracleDBRequest(sb.ToString, CommandType.Text)
status = connection.Execute(request, result, trace)
dv = New DataView(result.DataSet.Tables(0))
If (ViewState("sortExp") IsNot Nothing) Then
dv = New Data.DataView(result.DataSet.Tables(0))
If (GridViewSortDirection = SortDirection.Ascending) Then
GridViewSortDirection = SortDirection.Descending
dv.Sort = CType(ViewState("sortExp") & DESCENDING, String)
Else
GridViewSortDirection = SortDirection.Ascending
dv.Sort = CType(ViewState("sortExp") & ASCENDING, String)
End If
Else
dv = result.DataSet.Tables(0).DefaultView
End If
Return dv
Catch ex As Exception
Finally
'ds.Dispose()
'dp.Dispose()
End Try
End Function
Public Property GridViewSortDirection() As SortDirection
Get
If ViewState("sortDir") Is Nothing Then
ViewState("sortDir") = SortDirection.Ascending
End If
Return CType(ViewState("sortDir"), SortDirection)
End Get
Set(ByVal value As SortDirection)
ViewState("sortDir") = value
End Set
End Property
Protected Sub tblAdministrators_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles tblAdministrators.PageIndexChanging
tblAdministrators.PageIndex = e.NewPageIndex
tblAdministrators.DataSource = GetData()
tblAdministrators.DataBind()
End Sub
Protected Sub tblAdministrators_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles tblAdministrators.Sorting
ViewState("sortExp") = e.SortExpression
tblAdministrators.DataSource = GetData()
tblAdministrators.DataBind()
End Sub
End Class
嘗試進入代碼並查看PageIndexChanging上的ViewState(「sortExp」)的值是什麼。這應該給你一個線索。 –
感謝您的提示。當我從第1頁到第2頁時,排序是「升序」,然後是2到3,它是「降級」。排序不保留。所以我的代碼真的有問題了! –