DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Trending

  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET - Client Side State Management - Hidden Fields

ASP.NET - Client Side State Management - Hidden Fields

By 
Gil Fink user avatar
Gil Fink
·
Jul. 14, 08 · News
Likes (0)
Comment
Save
Tweet
Share
138.2K Views

Join the DZone community and get the full member experience.

Join For Free

What are Hidden Fields? 

Hidden fields technique is widely used in ASP.NET programing. Hidden fields are html input control with hidden type that store hidden data in the html. An example for a hidden field can look like this:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />

In the example above, I chose to show the event target hidden field in order to indicate that even in postback mechanism hidden fields are being used. The data stored in a hidden field is available when the form is processed on the server or when we use javascript.

Hidden Fields Values

Hidden fields store only one value in their value property. The value is saved as a string and therefore in order to use it for other types you need to perform casting. 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. You can see it by clicking the right mouse button and then choosing View Source from the menu (if the operation is available). Be aware not to use hidden fields to store confidential data! The values has page context and therefore when you leave a page the data stored in the hidden fields is disposed.

Server Control Hidden Fields

There are two types of server control hidden fields -  System.Web.UI.WebControls.HiddenField and System.Web.UI.HtmlControls.HtmlInputHidden. Both types has the same primary Value property to hold the value of the hidden field. You should choose between these types whenever you need to use a server side hidden field (A note - The difference between HtmlControls and WebControls isn't in the context of this post). When you don't use server controls you can use the Request.Form NameValueCollection to get the hidden field value by providing the client
id of the hidden field. For example the code bellow will return the string value of the __EVENTTARGET hidden field:

string eventTarget = Request.Form["__EVENTTARGET"];

Hidden Fields Example

I got some requests for an example of how to use the hidden fields inside web forms. The example is very easy to understand. I have an html input with type hidden. In the first request I fill the input with a message value with javascript function. I use a button control to do a post back to the server and in the post back the value of the hidden
field is inserted into a label control. Pay attention that because I use an html hidden field that isn’t a server control after a second post back it’s value will be empty. The only reason that the label will still show a message is because
the label’s default value of the EnableViewState is true. The web page’s code:

<%@ Page="" Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs"    Inherits="AJAXEnabledWebApplication1._Default" EnableSessionState="False" %>   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">   <head runat="server">      <title>Untitled Page</title>      <script type="text/javascript">        function PutHtmlHiddenFieldValue(message)        {          var hidden = document.getElementById('HTMLHiddenField');          if (hidden != null)          {            hidden.value = message;          }        }      </script>   </head>   <body dir="ltr">      <form id="form1" runat="server">        <div>         <div>            <asp:Label ID="lblHTMLHiddenField" runat="server"></asp:Label>            <input id="HTMLHiddenField" type="hidden" name="HTMLHiddenField"/>         </div>         <div>            <asp:Button ID="btnForPostBack" runat="server" Text="Do Postback" />         </div>        </div>      </form>   </body>  </html>

On the server side:

public partial class _Default : System.Web.UI.Page   {        #region Consts         private const string SCRIPT_KEY = "HtmlHiddenFieldScript";         #endregion         #region Page Events         protected void Page_Load(object sender, EventArgs e)        {            // Insert message to the label of the html control hidden            // field if there is a value in the html hidden field            string message = Request.Form["HTMLHiddenField"];            if (!string.IsNullOrEmpty(message))            {               lblHTMLHiddenField.Text = message;            }        }         protected void Page_PreRender(object sender, EventArgs e)        {            // Register a startup script in order to fill the html            // hidden field with a message value            if (!ClientScript.IsClientScriptBlockRegistered("HtmlHiddenFieldScript") &&             !IsPostBack)            {               ClientScript.RegisterStartupScript(typeof(Page), SCRIPT_KEY,                     "PutHtmlHiddenFieldValue('Html hidden hello world');", true);            }        }         #endregion   }

Summary

To sum up the post, today I drilled down into the hidden fields technique. This technique is very popular and is used widely in ASP.NET. In the next post I'll continue the tour in the client side state management techniques.

ASP.NET

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Zero to AI Hero, Part 3: Unleashing the Power of Agents in Semantic Kernel
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: