ASP.NET Masterpages and viewstate
-
03-07-2019 - |
Question
I am looking to improve the performance of my site, not because it is performing badly but just as a general exercise. The usual suggestion for asp.net sites is to remove viewstate wherever possible. I believe this can be done by each control on a page separately or for the whole page.
My question is if I disable the page viewstate will this stop the viewstate of controls on a masterpage (as I understand it the masterpage is actually a control on the page).
Solution
Yes, the page is the originator of the page flow. Thus, disabling viewstate for the page takes the viewstate rendering out of the OnInit process. A better question would be why does disabling the viewstate for the master page do the same?
OTHER TIPS
There's an easy way to shrink all your viewstate.
Step 1. Create a new class that looks like this:
Imports System
Imports System.Web.UI
Public Class SessionPageStateAdapter
Inherits System.Web.UI.Adapters.PageAdapter
Public Overrides Function GetStatePersister() As System.Web.UI.PageStatePersister
Return New SessionPageStatePersister(Page)
End Function
End Class
Step 2. Add an App_Browsers
folder to your project.
Step 3. In your new App_Browsers
folder, add a new default.browser
file that looks like this.
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.Page" adapterType="[YourNamespaceGoesHere].SessionPageStateAdapter" />
</controlAdapters>
</browser>
</browsers>
When you run your pages now, you should find your viewstate size has dropped to a few bytes. The SessionPageStateAdapter class intercepts viewstate before it gets served to the browser and holds it in on the server in Session state. The bit of viewstate that still gets sent to the client is just an identifier that is used to reconstitute the original viewstate when the page gets posted back to the server.
You might find this article helpful:
http://msdn.microsoft.com/en-us/library/ms972976.aspx
Just a quick note on the side before optimising the site, have you optimised the server by making sure all the files are gzipped before being sent.
If not this will get you a nice boost before you even start tinkering with the page.