Search code examples
c#viewstatewebforms

Totally Disable ViewState in ASP.NET Web Forms


In an ASP.NET 4 Web Forms Web Application I want to completely disable ViewState for a TextBox control. I've searched the internet but couldn't find an answer that solved my problem.

So far I have tried setting the Page directive like this:

EnableEventValidation="false" ViewStateMode="Disabled" EnableViewState="false"

I've also set the TextBox control like this:

ViewStateMode="Disabled" EnableViewState="false"

Normally these 2 settings should be enough for disabling ViewState, but it is not.

Then, I wrote the following in the page's code-behind file:

protected override void SavePageStateToPersistenceMedium(object state) { }

protected override object LoadPageStateFromPersistenceMedium()
{
    return null;
}

This resulted in the ViewState value in the HTML to be null, but when I press the submit button, the value in the TextBox is still remembered by the browser.

How can I disable this functionality?


Solution

  • You are misunderstanding how ViewState is used.

    See this article for a full explanation. To quote:

    ViewState is not responsible for the population of values that are posted such as by TextBox controls (although it does play an important role)