Search code examples
asp.netviewstate

Is it possible to disable ASP ViewState via POST or in cookies?


I know you can disable ViewState via the methods described here, but I'm wondering if it's possible to disable it via a form input from a client.

For example can you POST a value like disable_view_state=1 or in cookies or something similar?


Solution

  • No, that is not possible.When the page first renders viewstate will always be there unless you disable viewstate at the control level or at page level.

    1. Since viewstate is already there, you cannot suddenly disable it by any means;
    2. ASP.Net will always look for the initial viewstate of page when you post back since it needs the viewstate to do page processing.

    Your best bet is to disable it at the page level by using following page directive at top of page markup, so when page first renders it doesn't have viewstate. Note that ViewStateMode="Disabled" setting in page directive for disabling view state for all control in the page.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterPage.aspx.cs"
        Inherits="RepeaterPage"  ViewStateMode="Disabled" %>
    
    <!DOCTYPE html>