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. H
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.
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.
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="" />
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.
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
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.