Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlInputHidden
ASP.NET Syntax ASP.NET Syntax for HTML Controls
Creates a hidden control.
Declarative Syntax
<input type=hidden id="accessID" runat="server"
value="contentsofhiddenfield"
>
For information on the individual members of this class, see HtmlInputHidden in the class library.
- This control does not require a closing tag.
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.
The following example shows how the Web Forms page framework supports saving view state information across requests using the HtmlInputHidden control.
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.
- 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>
- 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.
Web Forms Events and Handlers HtmlInputHidden Class