How to remove ViewState from ASP.NET web page and save in Session using SessionPageStatePersister

By default ViewState is saved inside the web page. In ASP.NET using SessionPageStatePersister ViewState can be saved inside Session instead. This helps in securing the ViewState as well as reducing the size of the web page.

The code to implement the same is overriding the PageStatePersister for the ASP.NET page as displayed below. PageStatePersister provides the base functionality for ASP.NET view state persistence mechanisms.

    protected override PageStatePersister PageStatePersister
            return new SessionPageStatePersister(this);

The number of ViewState’s that can be saved in Session can be changed by setting historySize as displayed below
<SessionPageState historySize =”20″/ >
The difference between web page containing ViewState and when ViewState is inside Session is displayed below. The rows marked in green have ViewState inside Session whereas cells marked in red have ViewState inside web page.

Atul Verma

I am working at Microsoft as a .NET Consultant. I have an experience of 5 years in .NET technologies and application packaging. My expertise include .NET Framework(4.0, 3.5, 3.0, 2.0), WPF, WCF,, AJAX, Web Services, Enterprise Library, Enterprise Applications, SharePoint 2010, Web Services, SQL Server (2008, 2005), Open Xml, MS Word Automation.