Loading, please wait...

A to Z Full Forms and Acronyms

Difference between ViewState and Hidden Fields in ASP.NET

In this article, we will learn about the key differences between ViewState and Hidden Field in ASP.NET

Difference between ViewState and Hidden Fields in ASP.NET

When a web application is developed, it is stateless by default. It means that values present or filled in the controls is lost as soon as the web page is reloaded because every time a page is requested by the client to the server,  the page goes through a series of processes in the server memory itself and once it is delivered to the browser(client), the page is removed from the server memory. It happens because of a single server, a new instance of the page is created every time it is requested, and thereafter of all the controls of the web page. once it is delivered to the browser, all the instances of the page elements are destroyed from the server memory. Therefore, to retain those values in the controls, we use state management techniques like ViewState and HiddenFields.Now, we will learn the difference between ViewState and HiddenFileds.

ViewState

ViewState is a client-side state management technique. ViewState is used to store user data on-page at the time of the postback of a web page.

ViewState holds the values of controls on a web page. It does not restore the value to control after page postback. ViewState can hold the value on a single web page, if we go to another page using response.redirect then the value in the ViewState will be lost.

  • 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.

Storing values in ViewState:

ViewState[“name”]=”TutorialsLink”;

Retrieve value from ViewState:

string value=ViewState[“name”].ToString();

Hidden Fields

The hidden field technique is also a client-side state management technique used in ASP.NET programming. Hidden fields are HTML input control with the hidden types that store hidden data in the HTML.

Hidden fields store only one value in their valuable property. The value is saved as a string and therefore in order to use it for other types you need to perform a casting operation. Hidden fields' data is submitted to the server only in HTTP post-operation. You can see the stored data easily by using the View Source operation of the browser by clicking the right button of the mouse. You should avoid using hidden fields to store confidential data as it can be accessed easily and it increases the chances of data to be stolen! 

The values in hidden fields have page context and therefore when you leave a page the data stored in the hidden fields are destroyed.

Store values in Hidden Field

<asp:HiddenFieldID="Val1" runat="server" /> 

Setting values in Hidden Field by code

Val1.Value = DateTime.Now.ToString();  

Retrieving value from Hidden Field

Val2.Text = Convert.ToString(Val1.Value);

The key differences between ViewState and HiddenFields:

ViewState

hidden field

· ViewState is a secure method to store data.

·HiddenFields is not a safe and secure method as the data value in it can be seen and accessed very easily.

· ViewState can store large amounts of data.

· You can store more than one value like Datatable and Dataset.

·Only a small amount of data can be stored.

· You can store more than one value in a hidden field, by serializing it.

·View state data is encrypted.

· The data in HiddenField is not encrypted.

·The data in the ViewState can not be changed by Client-Side code,i.e, JavaScript.

· It is possible to change the value by Client-side code in HiddenField.

 

A to Z Full Forms and Acronyms

Related Article