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..
Many many thanks .i solved my problem by this class.
ReplyDeleteHello...
ReplyDeleteNice post...:)Its simple and understandable
but what about session ?
ReplyDeleteso now session will load on server right ?
@Anonymous - You need to understand how viewstate works.See the following links.
ReplyDeletehttp://aspalliance.com/72
http://www.hanselman.com/blog/MovingViewStateToTheSessionObjectAndMoreWrongheadedness.aspx