asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputHidden

HtmlInputHidden Control Syntax

ASP.NET Syntax   ASP.NET Syntax for HTML Controls


Creates a hidden control.

Declarative Syntax

For information on the individual members of this class, see HtmlInputHidden in the class library.

Syntax Notes

  • This control does not require a closing tag.

Working with HtmlInputHidden

Hidden controls are used within HTML forms to embed non-visible information that will be sent to the server each time a user performs a post-back. The control itself is not rendered but its value is submitted with the form. This provides a mechanism for storing and delivering information to the receiving program without user interaction.

The Web Forms framework uses this feature of HTML to automatically store and restore the viewstate of server controls across roundtrips to the server.

The HtmlInputHidden control enables programming of the HTML <input type=hidden> element. This sample illustrates use of the HtmlInputHidden control.


Using Hidden Text Fields to Save View State Information Across Requests

The following example shows how the Web Forms page framework supports saving view state information across requests using the HtmlInputHidden control.

Using Hidden Text Fields to Save View State Information Across Requests
Run Sample | View Source

Each time the form is sent, the form submit handler stores the current string in the text box as the hidden field’s value. On each page load, the <span> displays the text stored in the hidden field, which actually is from the Web request immediately preceding the current request.

  1. The code for the form implements an HtmlForm, an HtmlInputHidden control, an HtmlInputText, an HtmlInputButton, and a <span runat="server"> control.
    <form runat="server">
       <input type=hidden id="hiddenField" 
          value="Initially none. Click again." runat="server">
    
       <p>Enter a string: <input id="myText" type=text 
          size=30 runat="server">
    
       <p><input type=submit value="Enter" 
          onServerClick="submitHandler" runat="server">
    
       <p><span id="Message" runat="server">
          This label will display the previously entered text.
       </span>
    </form>
  2. There are two event handlers here. If the page is a postback, the <span> control displays the value stored in the hidden field from the previous request. When the user enters text into the HtmlInputText control ( myText ) and clicks the submit button, the form handler stores this text box’s value as the hidden field’s new value.
    <script language="C#" runat="server">
    void Page_Load ( object Source, EventArgs e ) {
       if ( Page.IsPostBack ) {
          Message.InnerHtml = "Saved previous value: " + hiddenField.Value;
       }
    }
    
    void submitHandler ( object Source, EventArgs e ) {
       hiddenField.Value = myText.Value;
    }
    </script>

You can easily modify this functionality to suit your needs.

See Also

Web Forms Events and Handlers   HtmlInputHidden Class



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note