16

I have a site that features some pages which do not require any post-back functionality. They simply display static HTML and don't even have any associated code. However, since the Master Page has a <form runat="server"> tag which wraps all ContentPlaceHolders, the resulting HTML always contains the ViewState field, i.e:

<input
  type="hidden"
  id="__VIEWSTATE"
  value="/wEPDwUKMjEwNDQyMTMxM2Rk0XhpfvawD3g+fsmZqmeRoPnb9kI="
/>

EDIT: I tried both variants of setting EnableViewState on page level with no luck at all:

<%@ Page Language="C#" EnableViewState="false" %>
<%@ Page Language="C#" EnableViewState="true" %>

I realize, that when decrypted, this value of the input field corresponds to the <form> tag which I cannot remove because it is on my master page. However, I would still like to remove the ViewState field for pages that only display static HTML. Is it possible?

Kerido
  • 2,930
  • 2
  • 21
  • 34

8 Answers8

24

You could override Render and strip it out with a Regex.

Sample as requested. (NB: Overhead of doing this would almost certainly be greater than any possible benefit though!)

[edit: this function was also useful for stripping all hidden input boxes for using the HTML output as a word doc by changing the MIMEType and file extension]

protected override void Render(HtmlTextWriter output)
{
    StringWriter stringWriter = new StringWriter();

    HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
    base.Render(textWriter);

    textWriter.Close();

    string strOutput = stringWriter.GetStringBuilder().ToString();

    strOutput = Regex.Replace(strOutput, "<input[^>]*id=\"__VIEWSTATE\"[^>]*>", "", RegexOptions.Singleline);

    output.Write(strOutput);
}
Ken Forslund
  • 340
  • 2
  • 9
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • 1
    The NB should be written in bold, 20pt all caps instead of between parentheses. This is one of the top results in Google and people **will** use this code even though they shouldn't. I guarantee it. – Steven Liekens Jun 29 '16 at 10:58
20

Add following methods to the page:

        protected override void SavePageStateToPersistenceMedium(object state)
    {
        //base.SavePageStateToPersistenceMedium(state);
    }

    protected override object LoadPageStateFromPersistenceMedium()
    {
        return null; //return base.LoadPageStateFromPersistenceMedium();
    }

    protected override object SaveViewState()
    {
        return null;// base.SaveViewState();
    }

Result :

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />
jimjim
  • 2,414
  • 2
  • 26
  • 46
  • 1
    @Craig : you are welcome, but I have moved away from asp pages and mvc, I prefer Angularjs and webservices instead so that many workarounds in asp.net are not needed. – jimjim Sep 25 '13 at 01:04
4

In the <% @page... directive at the top of the page, add EnableViewState="False". That will prevent the ViewState for that particular page.

Jamie Chapman
  • 4,229
  • 5
  • 29
  • 47
3

The method suggested by Martin must be used very carefully; because it may cause unexpected behaviors in your pages as Martin pointed in parenthesis. I've actually experienced it. But there is another option to remove viewstate content from page safely.

This option gives you the ability to use viewstate without setting false, it also allows you to remove it from your pages. Please check the articles below:

1- http://www.eggheadcafe.com/articles/20040613.asp

2- http://aspalliance.com/72

There is a solution file zipped under the Peter's article [1] you can download. I recommend that you read the second article also referenced by Peter. This is a perfect solution to remove viewstate content from your page while using its capabilities.

Sezen
  • 447
  • 1
  • 5
  • 17
2

There will always be a ViewState. See this related question:

Why does __VIEWSTATE hidden field gets rendered even when I have the EnableViewState set to false

Community
  • 1
  • 1
Sean Moubry
  • 1,010
  • 1
  • 13
  • 22
1

in .net4 you can just remove the runat="server" from the form tag. But you can't use server controls inside the form tag once you remove it.

Mark
  • 2,380
  • 11
  • 29
  • 49
Susantha
  • 19
  • 3
0

ViewState is added only if an asp:Form is present in the page. Remove the Form, and the hidden field will not be rendered.

Beware: By doing this, you are also renouncing to have server-side event handlers, or any kind of PostBack events.

jcarrenog
  • 199
  • 1
  • 3
0

Or just use a simple jQuery line to remove the fields, if you're using AJAX-style postback requests...

$(".aspNetHidden").remove();

This removes the DIV encasing the hidden __VIEWSTATE fields.

andym0908
  • 151
  • 1
  • 1
  • 6
  • Simple and efficient: $(document).ready(function () { setTimeout(function () { $("#__VIEWSTATE").remove(); $("#__VIEWSTATEGENERATOR").remove(); }, 300); }); – Khurram Ishaque Apr 04 '17 at 14:04