Click here to access my new Blog http://blogs.msdn.com/ahmedfarrag

Thursday, July 13, 2006

Storing the ViewState in Session variables !!

Hi all,

i was working today on optimizing a page that took lots of time to load, the first thing anyone will think about is the images sizes, viewstate and caching .. i tried to remove the view state from the controls that didn't need it , but still , it didn't help alot, besides some controls like the datagrid needs the viewstate to function well, so i have decided to find another place to store the viewstate .. !!!

when you override the functions "LoadPageStateFromPersistenceMedium" and "SavePageStateToPersistenceMedium" in the page, you can then save the viewstate to a session variable, that way the viewstate will not be a burden on the size of the page no more :) , so finally i got the perfomance better :D ..

he is a code that does this.

protected override object LoadPageStateFromPersistenceMedium ()
{
string key = Request.RawUrl + "_VIEWSTATE";
object state = Session[key];
return (state == null) ?
base.LoadPageStateFromPersistenceMedium () : state;
}

protected override void
SavePageStateToPersistenceMedium (object viewState)
{
string key = Request.RawUrl + "_VIEWSTATE";
Session[key] = viewState;
}





Enjoy :)