ASP.NET Hidden Field

From JholJhapata

Hidden Field is non visual control in ASP.NET where we can save the value. This is one of the types of client-side state management tools. It stores the value between the roundtrip. Anyone can see Hidden Field details by simply viewing the source of document.

Hidden Fields are not encrypted or protected and can be changed by anyone. However, from a security point of view, this is not suggested. ASP.NET uses HiddenField control for managing the ViewState. So, don’t store any important or confidential data like password, etc. with this control.

Using Hidden Field

We developers mostly do not show an ID value of table like UserID, ItemID, etc. because users are not concerned with this kind of data. We store that information in HiddenFields to complete our process easily.

Events of Hidden Field

As a control, it has events, which are as follows:

EVENT TYPE DESCRIPTION
DataBinding Occurs when Server Control binds to a data source.
Disposed Occurs when Server Control is released from the memory.
Init Occurs when Server Control is initialized.
Load Occurs when server control get loaded on the page.
PreRender Occurs before Rendering the page.
UnLoad Occurs when Server Control is unloaded from memory.
ValueChanged Occurs when the value gets changed between the round-trip (postback).

ValueChanged event is server-side control. This event gets executed when the value of HiddenField gets changed between postback to the Server.

Now people avoid to use server side ValueChanged Event as all these things are possible through JavaScript or jQuery easily.

Store the value in Hidden Field

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

Retrieve the value from Hidden Field

   protectedvoid Page_Load(object sender, EventArgs e)
   {
       hdnfldCurrentDateTime.Value = DateTime.Now.ToString();
   }