User Id :    Password :      New Member   Forgot Password
 
Store Page ViewState in Session with asp.net
Description Store Page ViewState in Session with asp.net   No. of Views     665
  Rating     0
Author Jalpesh Vadgama   Posted On     26 Nov 2011
Tags ASP.NET    

Sample Code   Download Code

As we all know we any web page on the internet is state less. We have to write our own mechanism to maintain state between httprequest and httpresponse. Asp.net has great features called viewstate to maintain state during page postback. It will store the element state in hidden variables with _VIEWSTATE. It will look like below.

 

 

If you are having large amount of controls then you will have larger viewstate on the page and it will increase your html kb as well as your performance. So to increase the performance of our application we need to store this viewstate in other place. ASP.NET 2.0 has nice new class called SessionPagePersister which will store the viewstate in the session. You just need to override the PageStatePersister property in your page and your viewstate will be on the page.

Here is the code.

 

protected override PageStatePersister PageStatePersister 
{ 
get 
{ 
return new SessionPageStatePersister(this); 
} 
\


If you are having so many pages then you have to write that cod manually Instead of doing this you can use inheritance features of C#.NET to write the code in just one way and then inherit your page from that class. Following is the code for that. You can create a class called MyPage that class into all the pages here is the code for class MyPage .

 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
namespace TestBlogApplication 
{ 
public class MyPage:System.Web.UI.Page 
{ 
protected override PageStatePersister PageStatePersister 
{ 
get 
{ 
return new SessionPageStatePersister(this); 
} 
} 

} 
\

 

and Then inherit the class in your page like following.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace TestBlogApplication
{
public partial class Page1 : MyPage
{
protected void Page_Load(object sender, EventArgs e)
{


}
}
}

 

So that's it that way you can increase your application performance via maintaining the viewstate in session. Happy Codding..
 

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by