Code: Detail - (English)

ViewState Compress

Sometime we need to compact view state as code in this link. Click here to see this solution in with Visual

Last update: 15/04/2010       

'First we need to create a compress class as code below

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.IO.Compression
Public Class CViewState

    Public Function LoadPageStateFromPersistenceMedium(ByVal _CVIEWSTATE As String) As Object
        Dim compressedViewStateTextData As String
        Dim descompressedViewStateTextData As String
        Dim viewStateArrayData As Byte()
        Dim losFormatter As LosFormatter

        losFormatter = New LosFormatter()

        compressedViewStateTextData = _CVIEWSTATE

        viewStateArrayData = Convert.FromBase64String(compressedViewStateTextData)

        viewStateArrayData = New CCompactacao().Decompress(viewStateArrayData)

        descompressedViewStateTextData = Convert.ToBase64String(viewStateArrayData)

        Return losFormatter.Deserialize(descompressedViewStateTextData)

    End Function

    Public Function SavePageStateToPersistenceMedium(ByVal state As Object) As String

        ' Cria Objetos 

        Dim losFormatter As LosFormatter

        Dim stringWriter As StringWriter

        Dim unCompressedViewStateString As String

        Dim compressedViewStateString As String

        Dim bytes As Byte()

        ' Inicializa o LosFormatter 
        losFormatter = New LosFormatter()

        ' Inicializa o StringWriter 
        stringWriter = New StringWriter()

        Using (stringWriter)
            ' Realiza a Serialização 
            losFormatter.Serialize(stringWriter, state)

            ' Converte para String o Conteúdo 
            unCompressedViewStateString = stringWriter.ToString()

            ' Converte o Texto de Base64 para Array de Bytes 
            bytes = Convert.FromBase64String(unCompressedViewStateString)

            ' Realiza a Compressão 
            bytes = New CCompactacao().Compress(bytes)

            ' Converte o Array de Bytes para Base64 
            compressedViewStateString = Convert.ToBase64String(byte

            Return compressedViewStateString

        End Using

    End Function

End Class

'Now we'll use the created class to compact a viewstate page

Imports System.Data
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Partial Class My_WebForm_Aspx
    Inherits System.Web.UI.Page

    Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal state As Object)
        Dim _cViewState As New CViewState()

        ' Adiciona o Field do Tipo Hidden 
        ClientScript.RegisterHiddenField("_CVIEWSTATE", _cViewState.SavePageStateToPersistenceMedium(state))
    End Sub

    Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
        Return New CViewState().LoadPageStateFromPersistenceMedium(Request.Form("_CVIEWSTATE"))
    End Function


End Class

Users who have marked this routine as a favorite
01/05/2011 23:32:34
How many % this code can compress?

The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.