locked
Completely disabling viewstate RRS feed

  • Question

  • User-2133066373 posted

    Hi,
    I am busy trying to build a mobile web site with ASP.NET and for it to 100% comply with XHTML Mobile 1.2, I need to completely disable viewstate.
    At the moment I just have a very simple web site with one master.page and one content page (default.aspx). For both the master.page and default.aspx pages I have put 

    Hi,

    I am busy trying to build a mobile web site with ASP.NET and for it to 100% comply with XHTML Mobile 1.2, I need to completely disable viewstate.

    At the moment I just have a very simple web site with one master.page and one content page (default.aspx). For both the master.page and default.aspx pages I have put (inside page_load):


    Page.EnableViewState = false;


    But when I run my site through the http://ready.mobi tester, then it creates a failure for not having valid markup. The main reason for this, is that if you have a <form> element, then only specific content elements. The only allowed content elements are:

    The content of element type "form" must match "(h1|h2|h3|h4|h5|h6|ul|ol|dl|p|div|pre|blockquote|address|hr|table|script|noscript|fieldset)+".

    Even with all the viewstates disabled, I still get the following in my HTML for the page:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>
    </title> 
    </head> 
    <body>
    <form method="post" action="/default.aspx" id="aspnetForm">
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE1MjQ5ODA0NjlkZKVUdXajOAHB0AuXphqwCVhfazGq" /> 
    <div>
    <span id="ctl00_ContentPlaceHolder1_Label1">
    this is a test
    </span> 
    <input type="submit" id="btnSubmit" class="hidden" /> 
    </div> 
    </form> 
    </body> 
    </html>

    If you look at the HTML above, then you will see that ASP.NET still includes a VIEWSTATE inside a "hidden" <input> element.

    How do I prevent to get ANY viewstate included? Or alternatively, not getting the
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE1MjQ5ODA0NjlkZKVUdXajOAHB0AuXphqwCVhfazGq" />

    directly after the <form> tag, so that it will comply?

    I am using MVS 2008 standard, and ASP.NET 3.5.

    I will really appreciate any/all advice.

    Best Regards
    Jan

    Wednesday, April 21, 2010 8:40 AM

Answers

  • User-2133066373 posted

    I have found my mistake (if anyone else is interested). It should be:

    01.using System;   
    02.using System.Collections.Generic;   
    03.using System.Linq;   
    04.using System.Web;   
    05.using System.Web.UI;   
    06.using System.Web.UI.WebControls;   
    07.  
    08.namespace mobi   
    09.{   
    10.    public partial class mobile : System.Web.UI.MasterPage   
    11.    {   
    12.        public mobile()   
    13.        {   
    14.            this.Init += new EventHandler(Page_Init);   
    15.        }   
    16.        protected void Page_Init(object sender, EventArgs e)   
    17.        {   
    18.            this.EnableViewState = false;   
    19.        }   
    20.    }   
    21.}

    Best Regards

    Jan
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 22, 2010 5:27 AM

All replies

  • User-1666980993 posted

    Really disabling ViewState is not just a matter of setting the value to false.  Here is an article with details:

    http://www.ironspeed.com/articles/Disable%20View%20State%20for%20a%20Page/Article.aspx

    Wednesday, April 21, 2010 10:47 AM
  • User-2133066373 posted

    Hi dswersky,

    Thank you for the information, but please bear with me. (maybe this will indicate my limited abilities) but I do not get it:


    In the article, Pooja Daga shows the following code:

    public ShowOrdersTablePage()
    {
        this.Init += new EventHandler(Page_Init);
    }
     
    private void Page_Init(object sender, System.EventArgs e)
    {
        this.EnableViewState = false;
    }


    She mentions that "In this example, the page’s class name is ShowOrdersTablePage".

    What executes ShowOrdersTablePage()?


    For example, if I have to try and convert it to my test setup - should I use it in this way:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace mobi
    {
        public partial class mobile : System.Web.UI.MasterPage
        {
            public mobi()
            {
                this.Init += new EventHandler(Page_Init);
            }
            protected void Page_Init(object sender, EventArgs e)
            {
                this.EnableViewState = false;
            }
        }
    }


    Thank you for your help to understand this!

    Best Regards

    Jan

    Wednesday, April 21, 2010 12:49 PM
  • User-2133066373 posted

    I have found my mistake (if anyone else is interested). It should be:

    01.using System;   
    02.using System.Collections.Generic;   
    03.using System.Linq;   
    04.using System.Web;   
    05.using System.Web.UI;   
    06.using System.Web.UI.WebControls;   
    07.  
    08.namespace mobi   
    09.{   
    10.    public partial class mobile : System.Web.UI.MasterPage   
    11.    {   
    12.        public mobile()   
    13.        {   
    14.            this.Init += new EventHandler(Page_Init);   
    15.        }   
    16.        protected void Page_Init(object sender, EventArgs e)   
    17.        {   
    18.            this.EnableViewState = false;   
    19.        }   
    20.    }   
    21.}

    Best Regards

    Jan
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 22, 2010 5:27 AM
  • User-1509403801 posted

    Why can't you use <%Page EnableViewState="False" %> ? 

    Wednesday, October 6, 2010 5:01 PM
  • User-1714406321 posted

    How do you manage the server control values after doing this? How is this advantageous for mobile web application?


    Monday, October 11, 2010 7:45 AM
  • User-2133066373 posted

    Mobile do not necessary have a fast Internet connection. If you are stuck with GPRS only, then you theoretically can get 115 kbps, but in reality after all the overheads you probably get about 60 kbps. Then memory on the mobile device is also not in all cases as much as we would like it to be (remember that there are older phones out there as well that will visit your mobile web site).

    If the viewstate for each element is saved, then it amount to quite a lot of data that accompany your actual web page. So by removing it, you make it more usable for mobile devices.

    As for controlling values that you need to access in following pages, well it depends on the amount of data, but you can consider saving them in session variables, cookies, or as part of the query string.

    Thursday, October 14, 2010 2:36 PM
  • User-625417884 posted

    QUOTE:wahidalimi wrote "10-06-2010 05:01 PM

    Why can't you use <%Page EnableViewState="False" %> ? "

    ===============================================

    because it worked up to VS 2005, since that good luck Charlie.

    VS 2008 and VS 2010 page renders with this annoying _viewstate fields.

    I wonder if that was done intentionaly by Microsoft or just another bug in VS?

    Wednesday, August 31, 2011 5:45 PM
  • User-102893888 posted

    I struggled with this for some time until a simple solution presented itself.

    Just remove it using javascript.

                var vs = document.getElementById("__VIEWSTATE");
                vs.parentNode.removeChild(vs);

    Wednesday, July 18, 2012 2:45 PM