Thursday, July 30, 2009

Store Page ViewState in Session with asp.net

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.

image

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.



Code Snippet


  1. protected override PageStatePersister PageStatePersister
  2. {
  3. get
  4. {
  5. return new SessionPageStatePersister(this);
  6. }
  7. \



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 .


Code Snippet


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;

  6. namespace TestBlogApplication
  7. {
  8. public class MyPage:System.Web.UI.Page
  9. {
  10. protected override PageStatePersister PageStatePersister
  11. {
  12. get
  13. {
  14. return new SessionPageStatePersister(this);
  15. }
  16. }

  17. }
  18. \



and Then inherit the class in your page like following.



Code Snippet


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. namespace TestBlogApplication
  8. {
  9. public partial class Page1 : MyPage
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {

  13. }
  14. }
  15. }




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

Share:

4 comments:

  1. Many many thanks .i solved my problem by this class.

    ReplyDelete
  2. Hello...

    Nice post...:)Its simple and understandable

    ReplyDelete
  3. but what about session ?
    so now session will load on server right ?

    ReplyDelete
  4. @Anonymous - You need to understand how viewstate works.See the following links.

    http://aspalliance.com/72

    http://www.hanselman.com/blog/MovingViewStateToTheSessionObjectAndMoreWrongheadedness.aspx

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews