Wednesday, December 31, 2008

Manage View State In Session

'Manage View state in Session

    Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)

 

        Dim VSKey As String

        'String that will hold the Unique Key

        'used to reference this ViewState data

 

        'Create the key based on the SessionID, on the Request.RawUrl

        'and on the Ticks representated by the exact time

        'while the page is being saved

 

        If Request.Form("__VIEWSTATE_KEY") Is Nothing Then

            VSKey = "VIEWSTATE_" & MyBase.Session.SessionID & "_" & _

                            Request.RawUrl & "_" & Date.Now.Ticks.ToString

        Else

            VSKey = Request.Form("__VIEWSTATE_KEY")

        End If

 

        'Store the viewstate in the Session.

        Session(VSKey) = viewState

 

        'Register a HiddenField on the Page,

        'that contains ONLY the UniqueKey generated.

        'With this, we'll be able to find with ViewState

        'is from this page, by retrieving these value.

        ClientScript.RegisterHiddenField("__VIEWSTATE_KEY", VSKey)

 

    End Sub

 

    Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object

 

        Dim VSKey As String 'ViewState UniqueKey

        VSKey = Request.Form("__VIEWSTATE_KEY")

        'Request the Key from the page and validade it.

 

        If Not VSKey.StartsWith("VIEWSTATE_") Then

            Throw New Exception("Invalid VIEWSTATE Key: " & VSKey)

        End If

 

        Return Session(VSKey)

 

    End Function

No comments:

Post a Comment