<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://jholjhapata.com/index.php?action=history&amp;feed=atom&amp;title=ASP.NET_View_State</id>
	<title>ASP.NET View State - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://jholjhapata.com/index.php?action=history&amp;feed=atom&amp;title=ASP.NET_View_State"/>
	<link rel="alternate" type="text/html" href="https://jholjhapata.com/index.php?title=ASP.NET_View_State&amp;action=history"/>
	<updated>2026-05-20T23:21:07Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://jholjhapata.com/index.php?title=ASP.NET_View_State&amp;diff=109&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default...&quot;</title>
		<link rel="alternate" type="text/html" href="https://jholjhapata.com/index.php?title=ASP.NET_View_State&amp;diff=109&amp;oldid=prev"/>
		<updated>2020-01-23T14:48:04Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.Please keep in mind though, that while cookies and sessions can be accessed from all your pages on your website, ViewState values are not carried between pages.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Points of ViewState ==&lt;br /&gt;
* ViewState does not hold the controls, rather it holds the values of the form controls and their corresponding ID's that would otherwise be lost due to a post back because they do not post with the form.  &lt;br /&gt;
* ViewState is not used to hold session data or to transmit data between pages.&lt;br /&gt;
* ViewState does not recreate the dynamically created controls of a page.&lt;br /&gt;
* It does not restore the values to the controls after a post back operation.&lt;br /&gt;
* ViewState for a control is disabled, still the value would be retained after a post back of the page occurs, for input controls like TextBox or DropDownList.&lt;br /&gt;
  &lt;br /&gt;
So, ViewState represents the state of a page when it was last processed on the web server.  It holds the values of a control that has been dynamically changed.&lt;br /&gt;
&lt;br /&gt;
== How does ViewState work? ==&lt;br /&gt;
&lt;br /&gt;
When the page is first created all controls are serialized to the ViewState, which is rendered as a hidden form field named __ViewState.  This hidden field corresponds to the server side object known as the ViewState.  ViewState for a page is stored as key-value pairs using the System.Web.UI.StateBag object.  When a post back occurs, the page de-serializes the ViewState and recreates all controls.  The ViewState for the controls in a page is stored as base 64 encoded strings in name - value pairs.  When a page is reloaded two methods pertaining to ViewState are called, namely the LoadViewState method and SaveViewState method.  The following is the content of the __ViewState hidden field as generated for a page in my system.&lt;br /&gt;
&lt;br /&gt;
[[File:ViewState.png|ViewState]]&lt;br /&gt;
&lt;br /&gt;
== ViewState can be enabled and disabled in any of the following ways ==&lt;br /&gt;
=== Control Level ===&lt;br /&gt;
To disable ViewState for a single control on a page, we need to set the EnableViewState property of the control to false:&lt;br /&gt;
    &amp;lt;asp:DropDownList ID=&amp;quot;DropDownList1&amp;quot; runat=&amp;quot;server&amp;quot; EnableViewState=&amp;quot;false&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;/asp:DropDownList&amp;gt;&lt;br /&gt;
=== Page Level ===&lt;br /&gt;
To disable ViewState for a single page, set the EnableViewState attribute in the @ Page directive to false:&lt;br /&gt;
    &amp;lt;%@ Page Title=&amp;quot;Home Page&amp;quot; Language=&amp;quot;C#&amp;quot;  EnableViewState=&amp;quot;false&amp;quot;  %&amp;gt;&lt;br /&gt;
We can also disable it by adding code in '''Class file''' of the page.&lt;br /&gt;
    private void Page_Init(object sender, System.EventArgs e)&lt;br /&gt;
    {&lt;br /&gt;
        this.EnableViewState = false;&lt;br /&gt;
    }&lt;br /&gt;
=== Application Level ===&lt;br /&gt;
To disable ViewState for a specific application, use the following element in the Web.config file.&lt;br /&gt;
  &amp;lt;configuration&amp;gt;&lt;br /&gt;
    &amp;lt;system.web&amp;gt;&lt;br /&gt;
      &amp;lt;pages enableViewState=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/system.web&amp;gt;&lt;br /&gt;
  &amp;lt;/configuration&amp;gt;&lt;br /&gt;
If you decide later that you need the ViewState for a specific page then you can turn it back on for just that page using a &amp;lt;location&amp;gt; element.&lt;br /&gt;
  &amp;lt;configuration&amp;gt;&lt;br /&gt;
    &amp;lt;system.web&amp;gt;&lt;br /&gt;
      &amp;lt;pages enableViewState=&amp;quot;false&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/system.web&amp;gt; &lt;br /&gt;
    &amp;lt;location path=&amp;quot;ActivePage.aspx&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;system.web&amp;gt;&lt;br /&gt;
        &amp;lt;pages enableViewState=&amp;quot;true&amp;quot; /&amp;gt;&lt;br /&gt;
      &amp;lt;/system.web&amp;gt;&lt;br /&gt;
    &amp;lt;/location&amp;gt;   &lt;br /&gt;
  &amp;lt;/configuration&amp;gt;&lt;br /&gt;
=== Machine Level ===&lt;br /&gt;
To disable ViewState for all applications on a Web server, configure the &amp;lt;pages&amp;gt; element in the Machine.config file.&lt;br /&gt;
  &amp;lt;Machine.config &amp;gt;&lt;br /&gt;
      &amp;lt;system.web&amp;gt;&lt;br /&gt;
         &amp;lt;pages enableViewState=&amp;quot;true&amp;quot; /&amp;gt;           &lt;br /&gt;
      &amp;lt;/system.web&amp;gt;&lt;br /&gt;
  &amp;lt;/Machine.config&amp;gt;&lt;br /&gt;
== Saving and Restoring Values to and from the ViewState ==&lt;br /&gt;
We can add any object or Primitive types to ViewState by following statements:&lt;br /&gt;
    ViewState[&amp;quot;key&amp;quot;] = obj;&lt;br /&gt;
    //Some Code&lt;br /&gt;
    obj = ViewState[&amp;quot;key&amp;quot;];&lt;br /&gt;
== Performance Issues ==&lt;br /&gt;
For a better performance in page rendering the size of the ViewState for a page should be minima. Remember that the data in the ViewState makes a round trip and incurs more network bandwidth usage.  So, ViewState should always be used carefully.  For pages and controls that do not require a post back at all, set the EnableViewState property of the page or the control of the page to false.&lt;br /&gt;
'''Note''': that only controls contained within a &amp;lt;form runat=server&amp;gt; tag in the .aspx page can store ViewState&lt;br /&gt;
== Security Issues ==&lt;br /&gt;
For security measures (to ensure that the ViewState is not tampered) one of the following two measures can be adopted.&lt;br /&gt;
&lt;br /&gt;
* EnableViewStateMac property&lt;br /&gt;
* Encryption of ViewState content&lt;br /&gt;
&lt;br /&gt;
The EnableViewStateMac property ensures a Machine Authentication Check (MAC).  This should be set at the page level or in the application’s web.config file.  When set, this property appends a hash code to the ViewState before rendering.  Whenever a post back occurs, this hash code is recalculated and checked with the one that is stored in the __ViewState hidden field of the form.  It they do not match, the page is rejected, thus ensuring that the ViewState is not tampered.&lt;br /&gt;
&lt;br /&gt;
To encrypt the contents of the ViewState, use the following in the machine.config file.&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;machineKey validation = &amp;quot;3Des&amp;quot; /&amp;gt;&lt;br /&gt;
        or&lt;br /&gt;
    &amp;lt; machineKey validation=&amp;quot;SHA1&amp;quot;/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
		
	</entry>
</feed>