View State and Server caching



My application is using ASP.NET 3.0 and My ASPX pages contain Master page,
and content page. The content page (.aspx) has various User controls (ascx).
My page view state is significantly large and I am looking for some smart way
to rfeduce the page size. So I indentified to manipulate ViewState. When page
is posted I will park the ViewState on Server and viewState will not move
back and forth. I am doing code as below in Page level by overrriding below

protected override void SavePageStateToPersistenceMedium(object viewState)
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
Cache.Add(str, viewState, null,
DateTime.Now.AddMinutes(Session.Timeout), TimeSpan.Zero,
CacheItemPriority.Default, null);
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");

protected override object LoadPageStateFromPersistenceMedium()
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE_"))
throw new Exception("Invalid viewstate key:" + str);
return Cache[str];

This is not working good for me. My pages get invalid viewState errors. I am
using Workflows too. Any idea how these methods can be better tuned when a
page contains multiple user controls.


Alvin Bruney [ASP.NET MVP]

So how is viewstate going to get to the client if you park it on the server?
It must move there eventually right? I suggest you look at reducing the size
of viewstate first but turning it off controls and pages that don't require
it. Remember, items like textbox don't need it. There's also control state
which is more efficient and tends to be lighter. However, I think you are
heading in the right direction for optimization.


Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question