Introduction
The purpose of this article is to show how to reduce the page size in ASP.NET applications. Another thing that we learn in this article is how to increase the performance of an ASP.NET application by reducing the response time of ASP.NET pages.
We have several ways to reduce the page size, the following are two of them:
- Page compression
- Storing Viewstate to the session or server-side.
By default Viewstate is persisted on the client side and that increases the page size. We can reduce this page size by storing viewstate on the server side.
In this article we will learn how to reduce the page size by storing the viewstate to the session.
Let's understand the problem first then we will rectify the problem.
Problem statement
Step 1: Create ASP.NET application
Step 2: Add the following class to the project:
[Serializable]
public class EmployeeClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Step 3: 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>
Step 4: 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();
}
}
}
Step 5: Understanding the preceding code:
- We 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 an object that is Serializable.
Step 6: Running the code
In the page we are getting a big list with 5000 rows. The current screen has only a few records because the page is very big.
Step 7: Right-click on the page to see the page size.
Pagesize: 859227 bytes
Step 8: Another way to see the page size is using F12 as in the following:
Problem
Pagesize: .82 mb
The size of the 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
Step 9: By default viewstate is saved in the browser in the form of a hidden field. If we save the viewstate to the seesion at the server then we can reduce the page size because in that case there would be no hidden fields in the ASP.NET page.
Step 10: Introduction of PageStatePersister class to the ASP.NET page
ViewState is saved by a descendant of 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 the Session object.
Step 11: 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.
Step 12: Add the following code to the page:
protected override PageStatePersister PageStatePersister
{
get
{
return new SessionPageStatePersister(Page);
}
}
Step 13: Running the code, the following will be the screen shot.
Page size : 334861.
Here the page size is reduced to 327 kb from .83 MB
Step 14: Comparison table
Step 15: Complete 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
http://msdn.microsoft.com/en-us/library/aa479403.aspx