Have you tried EnableViewState="False" and you still get the __VIEWSTATE hidden filed added?
If you want to use viewstate, then it's ok, but if I am not gonna use it, I don't want any information on my page that I don't need and that will only slow down the page load.

You can remove that hidden filed from the HTML with the code below.

            System.IO.StringWriter stringWriter = new System.IO.StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            string html = stringWriter.ToString();
            int startPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\"");
            if (startPoint >= 0)
                int endPoint = html.IndexOf("/>", startPoint) + 2;
                string viewstateInput = html.Substring(startPoint, endPoint - startPoint);
                html = html.Remove(startPoint, endPoint - startPoint);
                int formEndStart = html.IndexOf("</form>") - 1;

You can put it MasterPage's Render method, but maybe the best place would be BasePage's Render method.
Create a BasePage class that inherits MasterPage (if you're using master pages) and add Render method like this:

public partial class BasePages : System.Web.UI.MasterPage
protected override void Render(System.Web.UI.HtmlTextWriter writer)
// Add code from above

After that, in your Master page, inherit the BasePage like so:

public partial class MyMasterPage : MyNamespace.BasePages
// Some code

And you're done.
__VIEWSTATE hidden filed will be removed.

Note that if the hidden field attributes are in some other order or upper or lower case, this code will not work since it is looking for the exact string and it is case sensitive so it needs a little bit of tweaking to get the best results.
Category: asp.net
Bookmark and Share

Comment and make your thoughts visible

blog comments powered by Disqus