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:
- 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.
- protected override PageStatePersister PageStatePersister
-
- get
- {
- return new HiddenFieldPageStatePersister(Page);
- }
- }
- 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.
- protected override PageStatePersister PageStatePersister
- {
- get
- {
- return new SessionPageStatePersister(Page);
- }
- }
Procedure
- Create an ASP.NET application
- Add the following class to the project.
- [Serializable]
- public class EmployeeClass
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
- Add a GridView to the 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>
- Add the following code to the 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);
- }
- }
- 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.
- Running the code.
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.
- Right-click to see the viewstate in the hidden field as in the following:
- Right-click on the page to see the page size.
Pagesize : 859227 bytes
- Another way to see the page size is using F12 as in the following:
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
- 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.
- 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.
- 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.
- Add the following code to the page:
- protected override PageStatePersister PageStatePersister
- {
- get
- {
- return new SessionPageStatePersister(Page);
- }
- }
- Run the code and use view source to check the viewstate as in the following:
- Running the code, the following would be the screen shot.
Page size : 334861.
Here the page size is reduced to 327 kb from .83 MB
- Comparison table:
- Complete Code.
- 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 { get; set; }
- public string LastName { get; set; }
- }
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