Hy,
I'm interested in some general optimisation methods in an asp.net ajax project for Viewstate
(ex. to reduce the size of viewstate ,
or just speeding up the viewstate somehow,
or others this should be a general discussion :-) ).
So what kind of optimisation do you use for ViewState?
What possibilities are offered on Asp.net or Ajax framework?
@silky is right, disable it where ever you can. We try to keep it disabled on as many controls as possible.
Also, once you're using as little as possible, it might be worth looking at putting something like this in you pages (or better a base page class)
Protected Overrides ReadOnly Property PageStatePersister() As PageStatePersister
Get
Return New SessionPageStatePersister(Me)
End Get
End Property
But that depends on how many users you have and how much memory you have and it assumes that you are using sessions.
Oh and in C# it's:
protected override PageStatePersister PageStatePersister
{
get
{
return new SessionPageStatePersister(this);
}
}
Disabling View State of controls you dont need explicitly, for example there are many controls on your page, which may not require viewstate, like menus, some hyperlinks, some display statistics labels etc, turn off them a bit.
Also another thing you can do is, reduce the control ID length, this will improve the page size to a much better level.
Your control IDs are like following
PageContainer1_MyLoginContainerUserControl1_MyLoginForm1_MyUsername1
PageContainer1_MyCustomerDataGrid1_item0_MyCustomerNameLabel
I know it sounds little bad, but if you notice the grid/list items put so many big control IDs like this, we noticed that by reducing certain characters in ID also helps improving page size. In list/gridview etc it improves this by 30% to 40%, also nested user controls are bad as well, when you really dont think you need control's ID anymore, just put it 3 letter big max.
The best optimisation: store less stuff in it.
But you need to be specific. Are you experiencing a problem? If yes, with what controls? Your own? Others? Please expand.
I can be criticized for this: but in certain cases you can store viewstate in session by just override tho methods of Page.
To balance my karma I also suggest you to abandone both viewstate and session in future projects ;), for instance by trying Asp.NET MVC.
If you've never read TRULY Understanding ViewState, do that first.
As for AJAX helping with ViewState, I'm just speculating here, but if you have the entire page in an UpdatePanel you might be able to disable ViewState for the entire page
Well I was able to reduce my viewstates from 200-300KB to 62 BYTES by keeping it on the server and just passing a GUID back and forth.
I have written an article on how to do this by extending the HiddenFieldPageStatePersister that you can refer:
http://ashishnangla.com/2011/07/21/reducing-size-of-viewstate-in-asp-net-webforms-by-writing-a-custom-viewstate-provider-pagestatepersister-part-12/