Pages

Friday, March 23, 2012

Save View State in Session Variable

ViewState can be customized to be stored in a Session or Cache object. It
is done by overriding the 'SavePageStateToPersistenceMedium' and
'LoadPageStateFromPersistenceMedium' methods.


Add Following Two Override Method in to code behind file:

Step 1:

  Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
        If Session("ViewState") <> Nothing Then
            Return (New LosFormatter().Deserialize(DirectCast(Session("ViewState"), String)))
        End If
    End Function

Step 2:

    Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal state As Object)
        Dim los As New System.Web.UI.LosFormatter()
        Dim sw As New System.IO.StringWriter()
        los.Serialize(sw, state)
        Dim vs As String = sw.ToString()
        Session("ViewState") = Nothing
        Session("ViewState") = vs
    End Sub


Advantage of Moving ViewState object to Session Variable :

If the ViewState size is large, it increases the page size when it load in the browser and results in an increase in response time to load page performance.
Saving the
ViewState to a Session  can result in an improved response time as it reduce the ViewState object to Session but it use the server resource to store session information, so need to improve hardware information.

No comments:

ShareThis

Welcome

Welcome to Rajesh Prajapati, asp.net blog.
Here you can find some useful code and information about asp.net., c#, VB.net, SQL Server, Web Service, Web Designing etc