HiddenField In ASP.NET

Here, you will learn about the following topics:

  • What is HiddenField?
  • Using Hidden Field.
  • Events of HiddenField.
  • How to store the value in HiddenField.
  • How to retrieve the value from HiddenField.
  • Sample code of using HiddenField.

What is HiddenField?

HiddenField, as name implies, is hidden. This is non visual control in ASP.NET where you 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 HiddenField details by simply viewing the source of document.

HiddenFields are not encrypted or protected and can be changed by any one. 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 and credit card details with this control.
  1. <asp:HiddenFieldID="HiddenField1" runat="server" />  
Use of HiddenField

We developers mostly do not show an ID value of table like ProductID, MemberID because users are not concerned with this kind of data. We store that information in HiddenFields and complete our process very easily.

Events of HiddenFields

As a control, it should have events. HiddenFields has the following events.

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.

Nowadays, people avoid using server side ValueChanged Event because all this things are possible through JavaScript or jQuery very easily.

Store the value in HiddenField -

  1. <asp:HiddenFieldID="hdnfldCurrentDateTime" runat="server" />  
Set the value of HiddenField in code behind -
  1. protectedvoid Page_Load(object sender, EventArgs e) {  
  2.     hdnfldCurrentDateTime.Value = DateTime.Now.ToString();  
  3. }  
You can visit this article to see the right uses of HiddenField.

Retrieve the value from HiddenField -

  1. lblCurrentDateTime.Text = Convert.ToString(hdnfldCurrentDateTime.Value);  
Step by step implementation

Create a new ASP.NET Website project called “HiddenFieldExample”.


Right click on project and select Add-->Add New Item and select Web Form.


Add a new Web Form called “Default.aspx”.


Now, drag and drop the HiddenField Control on the page.


By default, the HiddenField control looks like this.
  1. <asp:HiddenFieldID="HiddenField1" runat="server" />  
Default.aspx Code
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>  
  2.     <!DOCTYPEhtml>  
  3.     <htmlxmlns="http://www.w3.org/1999/xhtml">  
  4.         <headrunat="server">  
  5.             <title></title>  
  6.             </head>  
  8.             <body>  
  9.                 <formid="form1" runat="server">  
  10.                     <div>  
  11.                         <asp:HiddenFieldID="hdnfldCurrentDateTime" runat="server" />  
  12.                         <asp:LabelID="lblCurrentDateTime" runat="server" Text=""></asp:Label>  
  13.                     </div>  
  14.                     </form>  
  15.             </body>  
  17.             </html>  
Default.aspx.cs Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. publicpartialclass_Default: System.Web.UI.Page {  
  8.     protectedvoid Page_Load(object sender, EventArgs e) {  
  9.         hdnfldCurrentDateTime.Value = DateTime.Now.ToString();  
  10.         lblCurrentDateTime.Text = Convert.ToString(hdnfldCurrentDateTime.Value);  
  11.     }  
  12. }  

That's it. Happy Coding!!!