Click here to Skip to main content
12,754,607 members (33,963 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads


27 bookmarked
Posted 27 Jun 2007

ViewState Serializer, Compressor & Encrypter

, 26 Sep 2009 CPOL
It's a very complete and robust processor of ViewState, it allows: to select the way of serialization, compression and encryption optionally.
Public Class formTest1
    Inherits ViewStateSerializer

#Region " C�digo generado por el Dise�ador de Web Forms "

    'El Dise�ador de Web Forms requiere esta llamada.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents lblViewSize As System.Web.UI.WebControls.Label
    Protected WithEvents chkGrid As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkTable As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkCrypt As System.Web.UI.WebControls.CheckBox
    Protected WithEvents chkOps As System.Web.UI.WebControls.CheckBox
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents lblLoadTime As System.Web.UI.WebControls.Label
    Protected WithEvents lblSaveTime As System.Web.UI.WebControls.Label
    Protected WithEvents lblOriginal As System.Web.UI.WebControls.Label

    'NOTA: el Dise�ador de Web Forms necesita la siguiente declaraci�n del marcador de posici�n.
    'No se debe eliminar o mover.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: el Dise�ador de Web Forms requiere esta llamada de m�todo
        'No la modifique con el editor de c�digo.
    End Sub

#End Region

    Dim dt As DataTable 'for a big table

    'Dim x As New ViewStateSerializer(True, False) You can try a manual mode

#Region "Overrides, not needed for a real app, only for this example"

    Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
        Dim time As Date = Date.Now
        Dim out As Object = MyBase.LoadPageStateFromPersistenceMedium()
        Session("LoadTime") = New TimeSpan(Date.Now.Ticks - time.Ticks)
        Return out
    End Function

    Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
        Dim time As Date = Date.Now
        lblSaveTime.Text = FormatNumber(New TimeSpan(Date.Now.Ticks - time.Ticks).TotalSeconds, 6) & " s"

        'calculate the original
        Dim formatter As New LosFormatter
        Dim writer As New System.IO.StringWriter
        formatter.Serialize(writer, viewState)
        lblOriginal.Text = FormatNumber(writer.ToString.Length / 1024, 3) & " KBytes"
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Response.Expires = -1 'important ?!

        If Not IsPostBack Then
            lblViewSize.Text = "Enable Javascript"
            lblLoadTime.Text = "Press 'Do Postback' Button"
            If Not Request.QueryString("EnCrypt") Is Nothing Then
                chkCrypt.Checked = IIf(Request.QueryString("EnCrypt").ToLower.Equals("true"), True, False)
            End If

            If Not Request.QueryString("Optimize") Is Nothing Then
                chkOps.Checked = IIf(Request.QueryString("Optimize").ToLower.Equals("true"), True, False)
            End If
            'set the configuration, one time please :D
            SetViewStateValues(chkCrypt.Checked, chkOps.Checked)
        End If

        'get a aleatory table
        If Session("dt") Is Nothing Then
            dt = Table
            Session("dt") = dt
            dt = Session("dt")
        End If

        ' Script that dumps the size of page viewstate
        RegisterStartupScript("lblViewSize", "<SCRIPT>var v = document.all('__VSTATE').value.length / 1024;" & _
            "document.all('lblViewSize').innerHTML = addCommas(v.toFixed(3)) + ' KBytes';</SCRIPT>")

        If Not Session("LoadTime") Is Nothing Then
            lblLoadTime.Text = FormatNumber(DirectCast(Session("LoadTime"), TimeSpan).TotalSeconds, 6) & " s"
        End If

    End Sub

#Region "Check Boxes: CheckedChanges Events"

    Private Sub values_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCrypt.CheckedChanged, chkOps.CheckedChanged
        Response.Redirect("formTest.aspx?EnCrypt=" & chkCrypt.Checked.ToString & "&Optimize=" & chkOps.Checked.ToString)
    End Sub

    Private Sub chkGrid_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGrid.CheckedChanged
        If chkGrid.Checked Then
            DataGrid1.DataSource = dt
            DataGrid1.Visible = True
            DataGrid1.DataSource = Nothing
            DataGrid1.Visible = False
        End If
        lblViewSize.Text = "Press 'Do Postback' Button"
    End Sub

    Private Sub chkTable_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTable.CheckedChanged
        If chkTable.Checked Then
            viewstate("dt") = dt
            viewstate("dt") = Nothing
        End If
        lblViewSize.Text = "Press 'Do Postback' Button"
    End Sub

#End Region

#Region "1000 Rows Datatable Generation"
    Shared r As System.Random = New Random
    Private Shared ReadOnly Property GetAName() As String
            Dim al As New ArrayList
            al.Add("John Doe")
            al.Add("Perry White")
            al.Add("Henry James Junior Ford")
            al.Add("Bill Norton")
            al.Add("Michal Johnathan Stewart ")
            al.Add("George Wilson")
            al.Add("Steven Edwards")
            Return al(r.[Next](0, al.Count)).ToString()
        End Get
    End Property
    Private Shared ReadOnly Property Table() As DataTable
            Dim dt As New DataTable
            dt.Columns.Add("Date of Birth", GetType(DateTime))
            dt.Columns.Add("Phone Number")
            dt.Columns.Add("Mobile Phone")
            dt.Columns.Add("Document ID")
            For x As Integer = 0 To 1000

                Dim dr As DataRow = dt.NewRow()
                dr("ID") = x.ToString()
                dr("Name") = GetAName
                dr("Date of Birth") = New DateTime(r.[Next](1940, 1984), r.[Next](1, 12), r.[Next](1, 28))
                dr("Phone Number") = "555-" + r.[Next](100000, 999999).ToString()
                dr("Mobile Phone") = "444-" + r.[Next](100000, 999999).ToString()
                dr("Document ID") = r.[Next](10000000, 99999999).ToString()

            Return dt
        End Get
    End Property
#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Sermicro
Spain Spain
My life in programming has been long, begins from the 6 years of age with Basic, I have knowledge of C++, Javascript, ASP .NET, Cisco CCNA, among others.

One of my pastimes in the programming, is cryptology and systems security

One of my recognized works is P2PFire, other smaller projects like utilities for Chats

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 26 Sep 2009
Article Copyright 2007 by ModMa
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid