Back to Basics – ASP.NET Page State Persister
Back to Basics – ASP.NET Page State Persister
Yesterday I was consulting
at
a client. The main
problems that the client
currently has are performance
issues. One of the problems
I found was the abuse of
ViewState without knowing
the consequences. I was
trying to reduce the ViewState for the pages and because of the
spaghetti code of the client’s application I had no chance in doing so.
I needed much more time to solve the problem with a proper solution.
Meanwhile, the client’s customer wanted results immediately so I used
the ASP.NET page state persiser to solve the matter currently and
instructed the developers how to start to solve their ViewState abuse.
What is ASP.NET Page State Persister?
ASP.NET supports a way to change the location in which the ViewState
data is stored. The default behavior to persist the ViewState is in a hidden
field. This behavior is enabled by the HiddenFieldPageState class. The
HiddenFieldPageState inherit from the PageStatePersister abstract class.
There is another implementation of the PageStatePersister which is the
SessionPageStatePersister. The SessionPageStatePersister enables us to
save the ViewState data in the session. Saving the ViewState in the session
consumes server resources (memory for example), can cause a lot of
troubles in web farms (using backend database for session can decrease
these problems) and makes troubles with session timeouts. In my situation
the change to SessionPageStatePersister is less painful because the client
uses only one server for the application and there aren’t a lot of users. But
even so you should always think before you make such a drastic change.
How to Use SessionPageStatePersister Instead of HiddenFieldPageState?
As written early, the default persister is the HiddenFieldPageState.
The way to change this behavior is very simple. In the web page you should
override the PageStatePersister property. The next example code show
how to do it:
protected override PageStatePersister PageStatePersister
{
get
{
return new SessionPageStatePersister(this);
}
}
If you want to enable this behavior to all your web application you
can create a base page that all your pages will inherit from. In that
base page you will put the previous shown code.
I built a small sample application that can show you how to use
SessionPageStatePersister. You can download it from here.
When using the example with the default behavior we get the
following ViewState:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJNzE3NzE5MzAyD2QWAgIDD2QWAgIBDzwrAA0CAA8WBB4LXyFEYXRhQm91bmRnHgtfIUl0ZW
1Db3VudAIyZAwUKwABFgYeBFR5cGUZKwEeBE5hbWUFBEl0ZW0eCURhdGFGaWVsZAUBIRYCZg9kFmYCAQ9kFgJm
Dw8WAh4EVGV4dAUBMGRkAgIPZBYCZg8PFgI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jQxZGQCKw9kFgJmDw8WAh8FBQI0MmRkAiwPZBYCZg8PFgIfBQUCNDNkZAItD2QWAmYPDxYCHwUFAjQ0ZGQCLg9k
FgJmDw8WAh8FBQI0NWRkAi8PZBYCZg8PFgIfBQUCNDZkZAIwD2QWAmYPDxYCHwUFAjQ3ZGQCMQ9kFgJmDw8WAh8
FBQI0OGRkAjIPZBYCZg8PFgIfBQUCNDlkZAIzDw8WAh4HVmlzaWJsZWhkZBgBBQpndkludGVnZXJzDzwrAAoBCA
IBZEvrLIgnB5+ESxkyXyxYmMJVv4GP" />
After using SessionPageStatePersister we get the following ViewState:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPaA8FDzhjYjk2MjExNjlhMGUyZRgBBQpndkludGVnZXJzDzwrAAoBCAIBZIuK8e4SPfGzD0VbBhUyyjJeCfeW" />
Summary
Lets sum up, I explained today what is ASP.NET page state persister.
I also explained how to use the SessionPageStatePersister instead of
the default HiddenFieldPageState. This solution shouldn’t be done lightly.
You should think before you change the default behavior. In my situation
this solution will help the client currently but if the client wouldn’t
decrease the ViewState in the long run the solution will raise other
problems. Therefore, I made that change for now and instructed the
developers how to decrease the use of ViewState and in the long run
how to restore the HiddenFieldPageState persister.