How To Remember Form Values Without Postback

Wee Bubba

Newcomer
Joined
Jan 27, 2005
Messages
7
hello there.

i need to persist the form values of a particular web page so that if a user gets timed out then i can repopulate it when they return to that page. likewise, if a user fills half the form in then decides to browse a different page before returning later on, then i will also need to remember the values.

i dont believe that i can trap either of these events with a postback so at the moment i am thinking the best solution is to keep storing the form values inside a cookie every time the javascript 'onunload' event handler fires. i can then repopulate from the cookie when a user comes back to the page. i can enforce that cookies is on for users of my application as it has a private user base so thats not a problem. however ive heard that certain popup stopper applications block this event handler so its perhaps not the best way.

is there a better way?
 
You could store each value in a cookie when each form element loses focus. I believe you could use the onBlur event to tell when the user has finished with the element and its lost focus.
 
Create a class called globVars

Post this code into it:

Code:
Public Class globVars
#Region "Private Variables"
    Protected Shared Inst As globVars = Nothing                             'this is the self-setting key to individualize globVars
    Protected mIdentity As System.Security.Principal.WindowsIdentity        'this is the back-up identity determiner
#End Region

#Region "Private Functions"
    Private Sub New()
        mIdentity = System.Security.Principal.WindowsIdentity.GetCurrent    'on every new instance, reset the identity
    End Sub
#End Region
#Region "Read Only Properties"

    Public ReadOnly Property GetIdentity()
        Get
            Return mIdentity
        End Get
    End Property
#End Region
#Region "Public Functions"
    Public Shared Function GetInstance() As globVars
        If Inst Is Nothing Then
            Inst = New globVars
        End If
        Return Inst
    End Function
    Public Shared Sub Setup()
        Inst = New globVars
    End Sub

#End Region

In your global.asax, in the StartSession, invoke the class like this:

Code:
Dim glob as globVars
glob = globVars.getinstance
glob.Setup

Now set every variable you need to pass as a property in the new public property

Code:
   Private mUser As String

   Public Property sUser() As String
        Get
            Return mUser
        End Get
        Set(ByVal value As String)
            mUser = value
        End Set
    End Property

You can now set a value for sUser in one page and retrieve it in the next

The class DOES tend to persist when you kill the site, because SessionEnd and ApplicationEnd don't always work in .Net. Therefore you re-invoke it with every session

Other sessions will have no affect on your class, however, as is it is invoked against the windows identity
 
If you cannot rely on the page being posted back then your solution is probably the best. If someone is using some form of pop-up / cookie blocker then there is nothing you can do except 'educate' them to either allow cookies from your app or just live with the problem.
 
You can also perform an XmlHttp request in the background and post the form elements back to the server.
 
Back
Top