Monday 24 February 2014

Reduce viewstate size asp net

ViewState : Web is a stateless medium; i.e., states are not sustained between requests by default. A web page is formed every time a page is posted to the server. The ASP.NET framework provides us several ways to maintain the state. Where ViewState is the one of the way. ViewState. We can persist data during postbacks with the help of ViewState. Why do we call ViewState as client-side? Because the data is stored on the page, and if we move from one page to another, the data would be lost.

Where does the ViewState gets stored? : The ViewState is stored on the page in a hidden field named __viewstate.
Problem On ViewState : The ViewState is stored on the page in the form of a hidden variable. It’s always advised to use the ViewState as little as possible. We also have other ways to reduce the performance overhead.
How to Reduce ViewState Size: There are many ways to reduce the view state size
·         Either use the compression\decompression
·         Maintain the view state on another server.

I am demonstrating you one of the way to doing it by SessionPageStatePersister; The SessionPageStatePersister utilizes server sessions associated with the browser session to store the data.
There are pros and cons to consuming the SessionPageStatePersister. Using session in its place of a hidden field avoids an increase in the page size being shown to and from the browser. In many conditions, the page state is a major part of the overall markup. However, loading this data in session consumes valuable server resources. Hidden fields do not have an linked timeout the way sessions do.
In order to use a persist other than the default, you override the PageStatePersister property of the page and return an instance of another persister.
protected override PageStatePersister PageStatePersister
        {
            get
            {
                return new SessionPageStatePersister(this);
            }
        }
If you want to apply it throughout the application, you can create a derived class and there you defined the SessionPageStatePersister and Inherit that derived page throughout your applications.
   public class PagePersisterBasePage : Page
    {
        public PagePersisterBasePage()
        {
        }
        protected override PageStatePersister PageStatePersister
        {
            get
            {
                return new SessionPageStatePersister(this);
            }
        }



    }

No comments:

Post a Comment