我正在尝试编写代码,以便在更改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
答案 0 :(得分:3)
问题在于,无论何时调用GetData()函数,您的代码都会更改dataview的sort属性,即使它是由PageIndexChaging事件调用的。
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
您可以修改GetData()函数以接受SortDirection。然后,在您的tblAdministrators_PageIndexChanging和tblAdministrators_Sorting事件中,只需传递GridViewSortDirection。像这样:
Private Function GetData(sort As SortDirection) As Data.DataView
' your code here...
End Function
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(GridViewSortDirection)
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
GridViewSortDirection = If(GridViewSortDirection = SortDirection.Descending, SortDirection.Ascending, SortDirection.Descending)
tblAdministrators.DataSource = GetData(GridViewSortDirection)
tblAdministrators.DataBind()
End Sub