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 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 and credit card details with this control.
<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.
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.
Nowadays, people avoid using server side ValueChanged Event because all these things are possible through JavaScript or jQuery very easily.
Store the value in HiddenField
<asp:HiddenFieldID="hdnfldCurrentDateTime" runat="server" />
Set the value of HiddenField in code behind
protected void Page_Load(object sender, EventArgs e) {
hdnfldCurrentDateTime.Value = DateTime.Now.ToString();
}
You can visit this article to see the right uses of HiddenField.
Retrieve the value from HiddenField
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.
<asp:HiddenFieldID="HiddenField1" runat="server" />
Default.aspx Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYP Ehtml>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenFieldID="hdnfldCurrentDateTime" runat="server" />
<asp:LabelID="lblCurrentDateTime" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class_Default: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
hdnfldCurrentDateTime.Value = DateTime.Now.ToString();
lblCurrentDateTime.Text = Convert.ToString(hdnfldCurrentDateTime.Value);
}
}
Output
That's it. Happy Coding!!!