Hi,
I want to store data in session variables available to all pages in my application, after 20 minutes though they are lost in the trash.
I've been playing with a way of retaining session variables beyond session timeout. I was wondering if someone had any thoughts on a possible solution i've come up with?
Here it is...
1) Session vars are saved within the page when it loads and passed back to session vars before leaving.
2)A common property is used to Set and Get each variable.
This following code is added to a pagebase class, inherited by all pages.
Within the page we can now store and retrieve any variable we want using...
We must save the vars back into session vars before we postback or move to another page, simply by calling...
EnableViewState="false" cannot set at the page level otherwise the viewstate vars are lost on postback.
The code could be changed to use hidden fields as an alternative.
That's it.
It seems to work, but I'd really appreciate any comments you might have.
Thanks
Silo4
I want to store data in session variables available to all pages in my application, after 20 minutes though they are lost in the trash.
I've been playing with a way of retaining session variables beyond session timeout. I was wondering if someone had any thoughts on a possible solution i've come up with?
Here it is...
1) Session vars are saved within the page when it loads and passed back to session vars before leaving.
2)A common property is used to Set and Get each variable.
This following code is added to a pagebase class, inherited by all pages.
Code:
Protected Sub PageBase_Load(ByVal sender As Object, ByVal e As EventArgs)
'On the first page load save the session vars to viewstate
If Not IsPostBack Then
SetVarsToViewState()
End If
End Sub
Protected Property Var(ByVal VarName As String) As String
'Return the var
Get
If Session(VarName) = Nothing Then
'if the session var has been lost
Return ViewState(VarName)
Else
Return Session(VarName)
End If
End Get
'Save the var
Set(ByVal Value As String)
Session(VarName) = Value
ViewState(VarName) = Value
End Set
End Property
Protected Sub SetVarsToSession()
'Save all session vars to viewstate
Dim vsEnum As IDictionaryEnumerator = ViewState.GetEnumerator
Do While vsEnum.MoveNext
Session(vsEnum.Key) = ViewState(vsEnum.Key)
Loop
End Sub
Protected Sub SetVarsToViewState()
'Save all viewstate vars to session
Dim ssEnum As IEnumerator = Session.GetEnumerator
Do While ssEnum.MoveNext
Viewstate(ssEnum.Current) = Session(ssEnum.Current)
Loop
End Sub
Within the page we can now store and retrieve any variable we want using...
Code:
Dim MyVar As String = Var("Var1")
Var("Var1") = "Hello"
We must save the vars back into session vars before we postback or move to another page, simply by calling...
Code:
SetVarsToSession()
EnableViewState="false" cannot set at the page level otherwise the viewstate vars are lost on postback.
The code could be changed to use hidden fields as an alternative.
That's it.
It seems to work, but I'd really appreciate any comments you might have.
Thanks
Silo4