More efficient / preferred way to use Session variables?

I have an ASP DataGrid and I am applying its sort. Well, since I was looking at an example, they had a function similar to a function other than the name, on:

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, I'm trying to make shortcuts and make things “easier”, maybe this would be better:

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, when I thought about it, I thought that maybe I Session("SortDir")should make a request every time, and this may have some consequences or disadvantages. But I was not sure. Does anyone have links that would explain the best or preferred method. Thank.

+3
source share
3 answers

( ), , . , ! , , , , (-, , ..), .

+1

Session , . - .

-

Private Property _SortDir As Nullable(Of String) ''# I used "Nullable(Of" because I don't know if you are able to use "String?"
Public Property SortDir() As Nullable(Of String)
    Get
        Return _SortDir
    End Get
    Set
        _SortDir = value
    End Set
End Property

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand

    If SortDir.IsNullOrEmpty Or SortDir = "DESC" Then
        SortDir = "ASC"
    Else
        SortDir = "DESC"
    End If

    BindData(e.SortExpression & SortDir))
End Sub

, sortOrder , . , ... , , , ? ? , Session.

, , . , "" _VIEWSTATE ( ).

, , "" .

.

( AJAX).

+1

, rockinthesixstring:

(: # - VB!)

Private Property _SortDir As Nullable(Of String)
Public Property SortDir() As Nullable(Of String)
    Get
        Return _SortDir
    End Get
    Set
        Session("SortDir") = value
        _SortDir = value
    End Set
End Property

Private Sub dgTasks_SortGrid(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTasks.SortCommand

    If SortDir = "ASC" Then    'I think the outer If block was redundant''
        SortDir = "DESC"
    Else
        SortDir = "ASC"
    End If

    BindData(e.SortExpression & SortDir))
End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not Page.IsPostBack Then
        _SortDir = Session("SortDir")
    End If
End Sub

C & P'd rockinthesixstring, , , , .

0

Source: https://habr.com/ru/post/1788594/


All Articles