Click here to Skip to main content
Licence CPOL
First Posted 2 Sep 2007
Views 29,942
Downloads 203
Bookmarked 34 times

Save ViewState on the File System

By | 2 Sep 2007 | Article
An article on how to save the ViewState on the file system.

Introduction

One disadvantage of the ViewSate is it adds extra kilobytes of data to the payload. To overcome this disadvantage, it can be saved on the server.

Strategy

There are three options to save the ViewState on the server.

  1. MS SQL Server
  2. File System
  3. Memory

Using MS SQL Server is expensive. If you do not have MS SQL Server, you have to pay extra money and the speed of it is the lowest. Using memory is expensive too. Because memory is a scarce resource. So, I have chosen to save the ViewSate on the file system.

Using the code

.NET uses the SavePageStateToPersistenceMedium method to save the ViewSate and the LoadPageStateFromPersistenceMedium method to load the ViewSate. I will override these functions to save and load the ViewSate using the file system.

protected override void SavePageStateToPersistenceMedium(Object viewState)
{
    LosFormatter lf = new LosFormatter();
    StringWriter sw = new StringWriter();
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    string fileName = this.GenerateFileName();

    lf.Serialize(sw, viewState);
    sb = sw.GetStringBuilder();
    WriteFile(sb.ToString(), fileName);

    sb = null;
    lf = null;
    sw = null;
}
    
protected override Object LoadPageStateFromPersistenceMedium()
{
    Object deserializedValue = null;
    string fileName = GetFileName();
    LosFormatter lf = new LosFormatter();

    deserializedValue = lf.Deserialize(ReadFile(fileName));
    
    lf = null;

    return deserializedValue;
}

GenerateFileName() generates and registers a file name using the Session ID and a GUID. The ViewState is saved in this file. GetFileName() returns this registered file name.

Performance

Saving ViewState on the page

Screenshot - saveViewState1.jpg

Saving ViewState on the file system

Screenshot - saveViewState2.jpg

You can see that the page size and the load time decrease with this method.

Using the demo

You should change the ViewStateRootDirectory in the web.config according to your directory structure. The ASP.NET user must have write permission to this directory.

In the Session_End event, the ViewState storage directory is deleted. In the Application_Start event, ViewStateRootDirectory is loaded to the cache to increase performance instead of reading it from web.config every time.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ibrahimuludag

Web Developer

Turkey Turkey

Member

Ibrahim ULUDAG
Software Developer
ibrahimuludag@gmail.com
http://ibrahimuludag.blogspot.com/
http://www.ibrahimuludag.com/


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionUsing ASP.NET 2.0 PageStatePersister & a GUID key for the ViewState PinmemberhAshishNangla8:53 25 Jul '11  
GeneralNice! PinmemberSandeep Mewara2:59 21 Mar '10  
QuestionA small issue PinmemberIrfanRaza21:14 29 Oct '09  
GeneralBrilliant PinmemberLidya_USQ15:46 9 Jul '09  
QuestionSession_end event did not fire?? Pinmemberwissam11:16 4 May '09  
Generalwill this work on a web farm PinmemberTomP6911:02 26 Sep '07  
GeneralRe: will this work on a web farm Pinmemberibrahimuludag2:13 9 Oct '07  
GeneralOne problem that should be noticed PinmemberAndre Luiz V Sanches13:32 2 Sep '07  
GeneralRe: One problem that should be noticed PinmemberBrad Vin1:49 3 Sep '07  
GeneralRe: One problem that should be noticed Pinmembermerlin9813:56 3 Sep '07  
GeneralRe: One problem that should be noticed Pinmembershlomiw7:05 6 Sep '07  
GeneralRe: One problem that should be noticed Pinmemberibrahimuludag11:51 6 Sep '07  
GeneralRe: One problem that should be noticed PinmemberAndre Luiz V Sanches19:31 6 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 2 Sep 2007
Article Copyright 2007 by ibrahimuludag
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid