我正在研究一个带有数据分页的可排序列表视图。我的列表视图和分页工作正常,但我正在努力进行排序。排序本身很好;我可以按特定列(ASC 或 DESC)进行排序,但是在动态排序方面我遇到了问题,用户可以选择一列,有时还可以反转排序方向。
我的主要问题是当前在 Form_Load 事件处理程序和 Listview_Sorting 事件处理程序中触发了排序和分页数据的生成。理想情况下,我希望在表单(重新)加载和选择新数据页面时以一种方式处理列表视图的填充,并在用户单击列标题时以另一种方式(即当(重新)排序)。不幸的是,当排序事件被触发时,Form_Load 中的代码被执行,然后 ListView_Sorting 中的代码被执行。
最初,我准备放弃只是效率低下,但现在一些 Form_Load 代码正在破坏我在 Sorting 事件处理程序中所做的事情。
所以我的问题是......我如何将这些事件的处理分成两组?当页面第一次加载以及从我尝试对数据进行排序时分页数据时,如何运行一组代码?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Initilize the Sort Column and Direction
Dim LastColumn As String = If(Session("SortColumn") Is Nothing, "LastWriteTime", Session("SortColumn"))
Dim SortDirection As SqlClient.SortOrder
Dim SortDirections As Dictionary(Of String, SqlClient.SortOrder) = Session("SortDirections")
If SortDirections Is Nothing OrElse Not SortDirections.ContainsKey(LastColumn) Then
SortDirection = SqlClient.SortOrder.Descending
Else
SortDirection = SortDirections(LastColumn)
End If
Call GenerateSortedArray(LastColumn, SortDirection)
End Sub
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
'bind array to ListView
Me.lvwMSGs.DataBind()
End Sub
Private Sub lvwMSGs_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvwMSGs.ItemCommand
Dim file As FileInfo = New FileInfo(e.CommandArgument.ToString) '-- if the file exists on the server
If e.CommandName = "Stream" Then
If file.Exists Then 'set appropriate headers
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
Response.AddHeader("Content-Length", file.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.WriteFile(file.FullName)
Response.End()
Else 'if file does not exist
Response.Write("This file does not exist.")
End If
End If
End Sub
Public Sub lvwMSGs_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles lvwMSGs.Sorting
Call GenerateSortedArray(e.SortExpression, SqlClient.SortOrder.Ascending)
End Sub
Private Sub GenerateSortedArray(ByVal SortColumn As String, ByVal DefaultSortDirection As SqlClient.SortOrder)
Dim dirInfo As New DirectoryInfo(Server.MapPath(AppSettings.Item("ContentDir")))
Dim FileArrayList As New ArrayList(dirInfo.GetFiles("*.msg", SearchOption.TopDirectoryOnly))
Dim SortDirections As New Dictionary(Of String, SqlClient.SortOrder)
With FileArrayList
.TrimToSize()
SortDirections = Session("SortDirections")
If Session("SortDirections") Is Nothing OrElse SortDirections.ContainsKey(SortColumn) Then
'Create dictionary, set to default and store in Session variable
If Session("SortDirections") Is Nothing Then
SortDirections = New Dictionary(Of String, SqlClient.SortOrder)
End If
SortDirections(SortColumn) = DefaultSortDirection
Session("SortDirections") = SortDirections
'Sort data according to preferences
.Sort(New FileInfoComparer(SortDirections(SortColumn), SortColumn))
Else
'retrieve previous sort direction
SortDirections(SortColumn) = 1 - SortDirections(SortColumn)
'Sort data according to preferences
.Sort(New FileInfoComparer(SortDirections(SortColumn), SortColumn))
End If
End With
With Me.lvwMSGs
.DataSource = FileArrayList
.ItemPlaceholderID = "ItemPlaceholder"
End With
End Sub