0 Comments

You can disable viewstate on an ASP .NET 2.0 website by setting the enabledViewState=”false” on the configuration > system.web > pages node in the Web.config:

<
pages enableViewState="false">

Then you will have to fill dropdownlists etc. on every page postback in the OnInit event of the page and some times have to use the good old: this.Request.Form

string
selectedValue = this.Request.Form["ctl00$MainContentPlaceHolder$selectBatchDropDownList"];
The "ctl00$MainContentPlaceHolder$selectBatchDropDownList"  is the control UniqueID (selectedBatchDropDownList.UniqueID)

However, after reading some articles:
http://dotneteers.net/blogs/petersm/archive/2006/11/09/how-to-put-controlstate-into-viewstate-and-how-to-put-viewstate-into-session.aspx
http://weblogs.asp.net/ngur/archive/2004/03/08/85876.aspx 
http://blog.tatham.oddie.com.au/2008/12/18/how-i-learned-to-stop-worrying-and-love-the-view-state/

I re-enabled ViewState and started filling dropdownlists on the page “OnInit” event and saved the viewstate in the sessionstate.
This allowed me to use the normal code and reduced the viewstate size to 50 characters.

To persist viewstate to sessionstate use:

        /// <summary>
        /// Set viewstate to sessionstate
        /// </summary>
        protected override PageStatePersister PageStatePersister
        {
            get
            {
                return new SessionPageStatePersister(this);
            }
        }

In the web.config

<system.web>
      <browserCaps>
          <case>
              RequiresControlStateInSession=true
          </case>
      </browserCaps>


To completely remove viewstate information use:

/// <summary>
        /// Completly disable viewstate for performance
        /// </summary>
        /// <param name="viewState"></param>
        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
        }
        /// <summary>
        /// Completly disable viewstate  for performance
        /// </summary>
        /// <returns></returns>
        protected override object LoadPageStateFromPersistenceMedium()
        {
            return null;
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.