View State in ASP.NET Overview


The state of the page held with page and not with the server.

View state is enabled by default for all server controls
HHTP is a stateless protocol, hence the state of the control is not saved between post backs. The information is saved in HTML hidden fields. In other words it is a snapshot of the content page.
There are 2 ways  to maintain the viewstate

In the previous Article I Explained about  Inserting Images to Database

We can disable viewstate on page level ie.., no state will be kept for any control on the page.
To do this,



<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeFile="StudentInformationReport.aspx.cs" Inherits="Registration_StudentInformationReport" %>


I can control the viewstate by control by control basis for this I simple add EnableViewState property to the declaration of control.


<asp:TextBox ID="TXT1" runat="server" ViewStateMode="Enabled"></asp:TextBox>


Enableviewstate is property of server controls which is true by default.It means the state of the control is stored in encoded form in hidden textboxes on the page at the time of page submission .
When the page is reloaded the properties are restored from viewstate.
Encryption of ViewState:
ViewState encryption modes
1) Auto.
2)Always.
3)Never.

ViewStateEncryptionMode.Auto:
In this mode,Asp.net will encrypt the viewstate for a page if any control on the page requests it. Note that this means all of the viewstate is encrypted not just the viewstate for the control that requests it.
So encrypting the whole viewstate is faster than doing separate encryption operation if more than one control makes the request

ViewStateEncryptionMode.Never:
In this mode,Asp.net will not encrypt the viewstate, even if the application is set for encryption and controls on the page have requested it.
If you know that no data involved in the page needs to be encrypted, then it may be safe to set Never

ViewStateEncryptionMode.Always:
Asp.Net Doesn’t wait for a control in the page to request encryption.Viewstate is always
encrypted.When working with sensitive data, it is a good practice

2 Ways to set this property

<%@ Page Language="C#" AutoEventWireup="true" ViewStateEncryptionMode="Always" CodeFile="StudentInformationReport.aspx.cs" Inherits="Registration_StudentInformationReport" %>


In Web.Config file


<configuration>
  <system.web>
    <pages viewStateEncryptionMode="Always">     
    </pages>
  </system.web>
</configuration>



Summary :

View state is nothing more than a hidden input which holds a hash of control values. If you view the source of an ASP.NET web form you will see something like this:



<input type="hidden" name="__VIEWSTATE"
            value="CEbzzzEnmmz+Bc8IDFlnpgCLJ/HB00...>


0 Responses to “View State in ASP.NET Overview”

Post a Comment

Labels

Topics