Freigeben über


HtmlInputHidden Control

Creates a server-side control that maps to the <input type=hidden> HTML element and allows you to store information in a nonviewable control on the form.

<input type="hidden"
       id="programmaticID"
       value="contentsofhiddenfield"
       runat="server" >

Remarks

Use the HtmlInputHidden control to program against the <input type=hidden> HTML element. Although this control is part of the form, it is never displayed on the form. Since state is not persisted in HTML, this control is commonly used in conjunction with the HtmlInputButton and HtmlInputText controls to store information between posts to the server.

Note   This control does not require a closing tag.

Example

The following example demonstrates how to save view-state information across requests using the HtmlInputHidden control. The <span> control displays the text stored in the hidden field from the Web request immediately preceding the present request.

There are two event handlers. The first event occurs when the page is posted back to the server. The event handler takes the text stored in the hidden field from the previous post request and displays it in a <span> control. The second event occurs when the submit button is clicked. The event handler takes the contents of the text box and stores it in the hidden field on the Web page.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
    <script runat="server">
      Sub Page_Load(Source As Object, e As EventArgs)
         If Page.IsPostBack Then
            Span1.InnerHtml = "Hidden value: <b>" + HiddenValue.Value + "</b>"
         End If
      End Sub

      Sub SubmitBtn_Click(Source As Object, e As EventArgs)
         HiddenValue.Value = StringContents.Value
      End Sub
    </script>
</head>

<body>
    <h3>HtmlInputHidden Sample</h3>
    <form runat="server">
        <input id="HiddenValue" 
               type=hidden value="Initial Value" runat="server">
        Enter a string: 
        <input id="StringContents" type=text size=40 runat="server">
        <p>
        <input type=submit value="Enter" 
               OnServerClick="SubmitBtn_Click" runat="server">
        <p>
        <span id=Span1 runat="server">
           This label will display the previously entered text.
        </span>
    </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
    <script runat="server">
       void Page_Load(object Source, EventArgs e) 
       {
          if (Page.IsPostBack) 
          {
             Span1.InnerHtml="Hidden value: <b>" + 
                             HiddenValue.Value + "</b>";
          }
       }
       void SubmitBtn_Click(object Source, EventArgs e) 
       {
          HiddenValue.Value=StringContents.Value;
       }
    </script>
</head>

<body>
    <h3>HtmlInputHidden Sample</h3>
    <form runat="server">
        <input id="HiddenValue" 
               type=hidden value="Initial Value" runat="server">
        Enter a string: 
        <input id="StringContents" type=text size=40 runat="server">
        <p>
        <input type=submit value="Enter" 
               OnServerClick="SubmitBtn_Click" runat="server">
        <p>
        <span id=Span1 runat="server">
           This label will display the previously entered text.
        </span>
    </form>
</body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlInputHidden Class