View State and Server caching

  • Thread starter Thread starter abcd
  • Start date Start date
A

abcd

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
methods.

protected override void SavePageStateToPersistenceMedium(object viewState)
{
string str = "VIEWSTATE_" + Request.UserHostAddress + "_" +
DateTime.Now.Ticks.ToString();
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.

Thanks
 
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.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
 
Back
Top