Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of View State completely in .NET

Tags:

c#

.net

asp.net

How can I get rid of:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..."/>

Completely !

like image 944
Boris Smirnov Avatar asked Nov 14 '08 17:11

Boris Smirnov


People also ask

How do I get rid of ViewState?

ViewState can be easily disabled for a particular control by setting EnableViewState property to False. ViewState can be disabled for the whole Page i.e. all controls on the Page by setting the EnableViewState property to False in the @Page Directive.

What is disable or remove ViewState hidden field in ASP.NET page?

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

What is ASP.NET enable ViewState?

View state enables a server control to maintain its state across HTTP requests. View state for a control is enabled if all of the following conditions are met: The EnableViewState property for the page is set to true . The EnableViewState property for the control is set to true .

Where is ViewState data stored?

ViewState is stored in a hidden field on the page at client side. ViewState is transported to the client and back to the server, and is not stored on the server or any other external source.


1 Answers

You need to add the EnableViewState="false" to the @Page directive in the Default.aspx file.

<%@ Page Language="C#" AutoEventWireup="true"
Codebehind="Default.aspx.cs" Inherits="Sample._Default"
EnableViewState="false" %>

Then, add the following code to the Default.aspx.cs file. This removes the hidden field from the generated HTML.

    #region Disable ViewState
    protected override void SavePageStateToPersistenceMedium(object state)
    {
    }
    protected override object LoadPageStateFromPersistenceMedium()
    {
        return null;
    }
    #endregion
like image 72
Julio César Avatar answered Oct 14 '22 15:10

Julio César