Home > DeveloperSection > Articles > Understanding PageStatePersister Class in ASP.net

Understanding PageStatePersister Class in ASP.net


ASP.Net ASP.Net 
Ratings:
2 Comment(s)
 3173  View(s)
Rate this:

Understanding PageStatePersister  Class in ASP.net

 

Introduction

Through this article we will learn how we can save viewstate at client and server side.

By default viewstate save on clientside via hidden fields but we can save viewstate to server side as well. Another objective of this article is that we can reduce the page size if application page is very heavy. We can reduce the page size by saving viewstate at server side.

PageStatePersister class

The PageStatePersister class defines the base functionality needed to persist page state to some backing store. This class is associated with a particular Page instance and provides Load and Save methods (among other members). The PageStatePersister class is abstract, meaning it can't be used directly but rather must be extended. There are two classes that extend PageStatePersister in the .NET Framework 2.0:

 

Understanding PageStatePersister Class in ASP.net

 

 

a)       HiddenFieldPageStatePersister - the default page state persisting logic. Uses a hidden form field to persist page state.

Following is default code in asp.net page. We do need to insert this code explicitly. Because by default view state is saved to hidden field

              protected override PageStatePersister PageStatePersister

        {

            get

            {

                return new HiddenFieldPageStatePersister(Page);

            }

         }

b)       SessionPageStatePersister - stores page state in a session variable.

Following code need to be add on asp.net page. Here by using following code viewstate will save to session varibles.

            protected override PageStatePersister PageStatePersister

        {

            get

            {

                return new SessionPageStatePersister(Page);

            }

        }

 

Steps.

1.      Create asp.net application

2.      Add following class to project.

            [Serializable]

    public class EmployeeClass

    {

        public string FirstName { getset; }

        public string LastName { getset; }

     }

 

 

 

3.      Add Gridview to Asp.net page.

 

Code

<%@ Page Language="C#" AutoEventWireup="true"

CodeBehind="ViewstateTest.aspx.cs"

Inherits="testViewState.ViewstateTest" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server"

BackColor="White" BorderColor="#999999"

BorderStyle="Solid" BorderWidth="1px" CellPadding="3"

ForeColor="Black" GridLines="Vertical">

            <AlternatingRowStyle BackColor="#CCCCCC" />

            <FooterStyle BackColor="#CCCCCC" />

            <HeaderStyle BackColor="Black"

Font-Bold="True" ForeColor="White" />

            <PagerStyle BackColor="#999999" ForeColor="Black"

HorizontalAlign="Center" />

           

<SelectedRowStyle BackColor="#000099" Font-Bold="True"

ForeColor="White" />

            <SortedAscendingCellStyle BackColor="#F1F1F1" />

            <SortedAscendingHeaderStyle BackColor="#808080" />

            <SortedDescendingCellStyle BackColor="#CAC9C9" />

            <SortedDescendingHeaderStyle BackColor="#383838" />

        </asp:GridView>

    </div>

    </form>

</body>

</html>

 

 

 

 

4.      Add following code to page load

   public partial class ViewstateTest : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

           

            if (!IsPostBack)

            {

                List<EmployeeClass> EmpList = new List<EmployeeClass>();

                for (int i = 0; i < 5000; i++)

                {

                    EmpList.Add(new EmployeeClass {

FirstName = "DEVESH"+i, LastName = "OMAR" });

                }

                ViewState.Add("PersonList", EmpList);

                GridView1.DataSource = EmpList;

                GridView1.DataBind();

            }

        }

}

protected override PageStatePersister PageStatePersister

        {

            get

            {

                return new HiddenFieldPageStatePersister(Page);

            }

 }

 

5.      Understanding the above code

i)      We have created list of EmployeeClass and added 5000 objects to this list

ii)     We are using 5000 objects to make the page heavy so that we would encounter the problem.

iii)   We are adding this list to viewstate

iv)     And finally we are binding list of 5000 object to grdivew.

v)      To save the object to viewstate we need to make class of object as Serializable.

vi)     We are using HiddenFieldPageStatePersister for saving view state to client side.

Actually we do not need to insert this code because viewstate by default save to clientside.

 

6.      Running the code.

 

Understanding PageStatePersister Class in ASP.net

 

In page we are getting big list having 5000 rows.

Current screen have only few records because page is very big.

 

7.      Right click to see viewstate in hidden field

 

Understanding PageStatePersister Class in ASP.net

 

8.      Right click on page to see page size.

Understanding PageStatePersister Class in ASP.net

 

Pagesize : 859227 bytes

 

9.      Another way to see page size using f12

Understanding PageStatePersister Class in ASP.net

 

Problem

Pagesize: .82 mb

 

Size of page is very big because data of viewstate always save in hidden field on client side or web page.

 

Solution

 

10.  By default viewstate save on browser in form of hidden field.

If we save viewstate to seesion at server then we can reduce page size

because in that case there would be no hidden fields on asp.net page.

 

11.  Introduction of  PageStatePersister  class to Asp.net page

 

ViewState is saved by a descendant of PageStatePersister class.

his class is an abstract class for saving and loading ViewsState and there are

two implemented descendants of this class in .Net Framework, named HiddenFieldPageStatePersister

 and SessionPageStatePersister. By default HiddenFieldPageStatePersister is used to save/load ViewState information,

 but we can easily get the SessionPageStatePersister to work and save ViewState in Session object.

 

 

 

12.  But this technique will increase memory usage in server so it would be good approach for intranet based application where user density is low.

 

13.  Add following Code to page.

 

protected override PageStatePersister PageStatePersister

        {

            get

            {

                return new SessionPageStatePersister(Page);

            }

        }

 

14.  Running code and see view source to check viewstate

 

Understanding PageStatePersister Class in ASP.net

 

15.  Running the code, following would be screen shot.

 

 

Page size : 334861.

Understanding PageStatePersister Class in ASP.net

 

Here page size reduced to 327 kb from .83 MB

 

 

16.  Comparison  table

 

Page Size before

Page Size after

860KB or 859227 bytes

327 KB or 334861 Bytes

 

 

 

 

17.  Complete Code.

 

Understanding PageStatePersister Class in ASP.net

 

18.  Code

 

public partial class TestViewstate : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                List<EmployeeClass> EmpList = new List<EmployeeClass>();

                for (int i = 0; i < 5000; i++)

                {

                    EmpList.Add(new EmployeeClass { FirstName = "DEVESH" + i, LastName = "OMAR" });

                }

                ViewState.Add("PersonList", EmpList);

                GridView1.DataSource = EmpList;

                GridView1.DataBind();

            }

 

        }

        protected override PageStatePersister PageStatePersister

        {

            get

            {

                return new SessionPageStatePersister(Page);

            }

        }

 

    }

 

    [Serializable]

    public class EmployeeClass

    {

        public string FirstName { getset; }

        public string LastName { getset; }

     }

 

 

Conclusion

 

We learn following things in this article.

a.      PageStatePersister class

b.      SessionPageStatePersister

c.  HiddenFieldPageStatePersister

 

This approach is good for very big pages but has disadvantage too.

 


Understanding PageStatePersister Class in ASP.net

By John Smith on   2 years ago
nice article

Thanks

By Devesh Omar on   2 years ago
Thanks

Don't want to miss updates? Please click the below button!

Follow MindStick