I have an ASP DataGrid and I’m applying sorting to it. Well, as I was looking at an example, they had a function similar in function, different in name, to:
Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand
Dim strSortDirection As String = Session("SortDir")
If strSortDirection = Nothing Then
strSortDirection = " ASC "
Else
If strSortDirection = " ASC " Then
strSortDirection = " DESC "
Else
strSortDirection = " ASC "
End If
End If
Session("SortDir") = strSortDirection
BindData(e.SortExpression & strSortDirection)
End Sub
Well, me trying to make shortcuts and make things “easier” thought maybe this would be best:
Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand
If Session("SortDir") = Nothing Then
Session("SortDir") = " ASC "
Else
If Session("SortDir") = " ASC " Then
Session("SortDir") = " DESC "
Else
Session("SortDir") = " ASC "
End If
End If
BindData(e.SortExpression & Session("SortDir"))
End Sub
However, as I was thinking about it, I figured maybe Session("SortDir") has to make a request everytime and it could have some affect or drawbacks. But I wasn’t sure. Does anyone have any links that would explain the best or preferred method. Thanks.
Looking up the Session value twice (one read and one write) rather than four times seems clearly better. The performance difference won’t be noticable by the user, but all those redundant lookups would make most programmers very uncomfortable! And there are some occasions where this kind of thing could make a noticable difference, depeding on the amount of data stored in the collection, and the type of lookup performed (hash table, binary search, sequential search, etc.), so it’s probably not a good habit to get into.