Understanding PageStatePersister Class in ASP.Net

Introduction

Through this article we will learn how to save viewstate on the client and server side.

By default viewstate is saved on the client side using hidden fields but we can save viewstate on the server side as well. Another objective of this article is to reduce the page size if the application page is very heavy. We can reduce the page size by saving the viewstate on the 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 specific 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:

PagestatePersister class

  • HiddenFieldPageStatePersister: the default page state persisting logic. Uses a hidden form field to persist page state.

    the following is the default code in an ASP.NET page. We do need to insert this code explicitly, because by default the view state is saved to a hidden field.
    1. protected override PageStatePersister PageStatePersister  
    2.   
    3.    get  
    4.    {  
    5.        return new HiddenFieldPageStatePersister(Page);  
    6.    }  

  • SessionPageStatePersister: stores page state in a session variable.

    The following code needs to be add on the ASP.NET page. Here by using the following code the viewstate will be saved to session variables.
    1. protected override PageStatePersister PageStatePersister  
    2. {  
    3.     get  
    4.     {  
    5.         return new SessionPageStatePersister(Page);  
    6.     }  

Procedure

  1. Create an ASP.NET application
  2. Add the following class to the project. 
    1.  [Serializable]  
    2. public class EmployeeClass  
    3. {  
    4.     public string FirstName { getset; }  
    5.     public string LastName { getset; }  

  3. Add a GridView to the ASP.NET page.

    Code 
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewstateTest.aspx.cs" Inherits="testViewState.ViewstateTest" %>        
    2. <!DOCTYPE html>   
    3. <html xmlns="http://www.w3.org/1999/xhtml">   
    4. <head runat="server">   
    5.     <title></title>   
    6. </head>   
    7. <body>   
    8.     <form id="form1" runat="server">   
    9.     <div>   
    10.         <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical">   
    11.             <AlternatingRowStyle BackColor="#CCCCCC" />   
    12.             <FooterStyle BackColor="#CCCCCC" />   
    13.             <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />   
    14.             <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />   
    15.             <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />   
    16.             <SortedAscendingCellStyle BackColor="#F1F1F1" />   
    17.             <SortedAscendingHeaderStyle BackColor="#808080" />   
    18.             <SortedDescendingCellStyle BackColor="#CAC9C9" />   
    19.             <SortedDescendingHeaderStyle BackColor="#383838" />   
    20.         </asp:GridView>   
    21.     </div>   
    22.     </form>   
    23. </body>  
    24. </html> 
  4. Add the following code to the page load:
    1. public partial class ViewstateTest : System.Web.UI.Page       
    2.        protected void Page_Load(object sender, EventArgs e)   
    3.        {              
    4.            if (!IsPostBack)   
    5.            {   
    6.                List<EmployeeClass> EmpList = new List<EmployeeClass>();   
    7.                for (int i = 0; i < 5000; i++)   
    8.                {   
    9.                    EmpList.Add(new EmployeeClass { FirstName = "DEVESH"+i, LastName = "OMAR" });  
    10.                }   
    11.                ViewState.Add("PersonList", EmpList);   
    12.                GridView1.DataSource = EmpList;   
    13.                GridView1.DataBind();   
    14.            }  
    15.        }   
    16.    }     
    17.    protected override PageStatePersister PageStatePersister   
    18.    {   
    19.        get   
    20.        {   
    21.            return new HiddenFieldPageStatePersister(Page);   
    22.        }  
    23.   } 
  5. Understanding the code above:

    • We have created a list of EmployeeClasses and added 5000 objects to this list.
    • We are using 5000 objects to make the page heavy to create the problem.
    • We are adding this list to the viewstate.
    • And finally we are binding the list of 5000 objects to the grdivew.
    • To save the object to the viewstate we need to make a class of object as Serializable.
    • We are using HiddenFieldPageStatePersister for saving view state to the client side.

    Actually we do not need to insert this code because viewstate by default is saved to the clientside.

  6. Running the code.

    run

    In the page we are getting big list with 5000 rows. the current screen has only a few records because the page is very big.
     
  7. Right-click to see the viewstate in the hidden field as in the following:

    viewstate in hidden field
     
  8. Right-click on the page to see the page size.

    page size

    Pagesize : 859227 bytes
     
  9. Another way to see the page size is using F12 as in the following:

    see page size using f12

    Problem

    Pagesize: .82 MB

    The size of page is very large because the data of the viewstate is always saved in a hidden field on the client side or web page.

    Solution
     
  10. By default the viewstate is saved in the browser as a hidden field. If we save viewstate to the seesion in the server then we can reduce the page size because in that case there would be no hidden fields on the ASP.NET page.
     
  11. Introduction of the PageStatePersister class to an ASP.NET page.

    ViewState is saved by a descendant of a PageStatePersister class. This class is an abstract class for saving and loading ViewsState and there are two implemented descendants of this class in the .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 a Session object.
     
  12. But this technique will increase memory usage in the server so it would be a good approach for an intranet based application where the user density is low.
  13. Add the following code to the page:
    1. protected override PageStatePersister PageStatePersister  
    2. {  
    3.     get  
    4.     {  
    5.         return new SessionPageStatePersister(Page);  
    6.     }  

  14. Run the code and use view source to check the viewstate as in the following:

    view source to check viewstate

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

    Running the code

    Page size : 334861.

    size

    Here the page size is reduced to 327 kb from .83 MB
     
  16. Comparison table:

    Comparison table
     
  17. Complete Code.

    Complete Code

  18. Code:
    1. public partial class TestViewstate : System.Web.UI.Page   
    2. {   
    3.     protected void Page_Load(object sender, EventArgs e)  
    4.     {   
    5.         if (!IsPostBack)   
    6.         {   
    7.             List<EmployeeClass> EmpList = new List<EmployeeClass>();   
    8.             for (int i = 0; i < 5000; i++)   
    9.             {   
    10.                 EmpList.Add(new EmployeeClass { FirstName = "DEVESH" + i, LastName = "OMAR" });   
    11.             }   
    12.             ViewState.Add("PersonList", EmpList);   
    13.             GridView1.DataSource = EmpList;   
    14.             GridView1.DataBind();   
    15.         }    
    16.     }   
    17.     protected override PageStatePersister PageStatePersister   
    18.     {   
    19.         get   
    20.         {   
    21.             return new SessionPageStatePersister(Page);   
    22.         }   
    23.     }    
    24. }    
    25. [Serializable]   
    26. public class EmployeeClass   
    27. {   
    28.     public string FirstName { getset; }   
    29.     public string LastName { getset; }   
    30. }    

Conclusion

We learned the following things in this article.

  • PageStatePersister class
  • SessionPageStatePersister
  • HiddenFieldPageStatePersister

This approach is good for very large pages but has disadvantages too.

References

ASP.NET 2.0 Page State Persister