Updated March 30, 2023
Introduction to ASP.NET Hidden Field
The hidden field control is inherited from the System.Web.UI.WebControls class is a control that is not visible to the user and stores information that needs to be persisted during the server round trip. The details stored in the Hidden Field control can be seen as simple by viewing the source of the document.
Syntax with Properties
Following is a syntax:
Syntax:
<asp:HiddenField EnableTheming="True|False" EnableViewState="True|False" ID="string" OnDataBinding="DataBinding event handler" OnDisposed="Disposed event handler" OnInit="Init event handler"
OnLoad="Load event handler" OnPreRender="PreRender event handler" OnUnload="Unload event handler” OnValueChanged="ValueChanged event handler" runat="server" SkinID="string" Value="string"
Visible="True|False" /><
The following code creates a hidden field with ID “HdbFldDemo” which stores the value “value to be stored” in its Value property. This property will hold the value in the User’s browser even after the page is posted to the server and information is returned. The OnDataBinding event called the method to bind the control to the database table, from which the data will be retrieved on the call of this event. The OnDisposed event is called to dispose of any object attached to the control. The OnPreRender event is used to set any properties before the page is rendered in the browser.
<asp:HiddenField EnableTheming="True " EnableViewState="True " ID="HdbFldDemo" OnDataBinding="DataBinding event handler" OnDisposed="Disposed event handler” OnInit="Init event handler"
OnLoad="Load event handler" OnPreRender="PreRender event handler" OnUnload="Unload event handler” OnValueChanged="ValueChanged event handler" runat="server" SkinID="SkinDemo" Value="value to be stored"
Visible="false" />
Extracting and storing values in hidden fields from code-behind files
HdbFldDemo.Value = abc
Label.Text = Convert.ToString(HdbFldDemo.Value);
Properties:
The hidden field has the following properties
- EnableTheming: This Boolean property decides whether the themes should apply to this control or not.
- ID: This string property stores the value for the control, using which it will be accessed throughout the program.
- SkinID: The string property gets or sets the name for the skin property.
- Value: The string stores the actual value the hidden field will hold.
- Visible: The Boolean property set whether the control will be rendered on the web page
- ViewStateMode: View state mode property sets or returns the current view state mode of the control.
- Parent: Returns the reference to the parent control if any on the page.
- EnableViewState: Sets the value to true to set the view state of the control or can be used to return the value of the current settings.
- Context: Return the current HTTPContextObject of the control for the web page.
Examples of ASP.NET Hidden Field
1. Launch Visual Studio -> New -> Project -> ASP.NET web site.
2. Right-click on the project in the properties window -> select Add -> Click on Add new item.
3. Add a Web Form. Rename it to Default.aspx
4. In the Default.cs file, as following code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID ="HiddenFieldDemo" Visible="false" runat="server"/>
</div>
</form>
</body>
</html>
5. The hidden field can also be added declaratively by dragging and dropping the hidden field control from asp.net toolbox.
6. The values for the properties can be set from the properties window as:
7. Add a label to the code where the value of the hidden field will be displayed.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID ="HiddenFieldDemo" runat ="server"/>
<label id ="lblTime" runat="server"></label>
</div>
</form>
</body>
</html>
8. In code-behind file add code that contains the current date and time added to the value property of the hidden field control.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HiddenFieldDemo.Value = DateTime.Now.ToString();
lblTime.Text = Convert.ToString(HiddenFieldDemo.Value);
}
}
}
The current date and time will be stored in the hidden field on the Page_Load event and then displayed in the label controls’ text property.
Conclusion
The hidden field control is part of the client-side state management technique used by ASP.NET. All web applications use HTTP protocol which is stateless. The term stateless means once, the information is posted to the server, the state of the controls is lost. To ensure that client that sends multiple requests the state management techniques are used. The client-side state management stores data on the user’s browser which helps in improving the scalability of the webserver. As these hidden fields are non-visual controls and have the facility for encryption or kept in any protection, it is advisable not to use Hidden Fields to store any important data like view state, passwords, account details, etc.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET Hidden Field” was beneficial to you. You can view EDUCBA’s recommended articles for more information.