views:

347

answers:

5

Hi everybody,

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?

+3  A: 

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

Jamie Chapman
This is already the case. However, the field is still there. I updated the question
Kerido
A: 

Doesn't EnableViewState help?

wRAR
Nope. The field renders as usual
Kerido
A: 

you can set at the page level like..

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" %>
Muhammad Akhtar
It doesn't help. The input field renders as usual.
Kerido
+1  A: 

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!)

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);
}
Martin Smith
Care to provide a sample?
Kerido
A: 

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

http://stackoverflow.com/questions/283082/why-does-viewstate-hidden-field-gets-rendered-even-when-i-have-the-enableviewst

Sean