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 ContentPlaceHolder
s, 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?
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.
Or just use a simple jQuery line to remove the fields, if you're using AJAX-style postback requests...
This removes the DIV encasing the hidden __VIEWSTATE fields.
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
Add following methods to the page:
Result :
in .net4 you can just remove the
runat="server"
from theform
tag. But you can't use server controls inside theform
tag once you remove it.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]