Freigeben über


HtmlInputHidden Server Control Declarative Syntax

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"
    EnableViewState="False|True"
    Id="string"
    Visible="False|True"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed event handler"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnServerChange="OnServerChange event handler"
    OnUnload="OnUnload event handler"
    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" %>
<!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>
<title>HtmlInputHidden Control</title>

    <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 id="Form1" runat="server">
        <input id="HiddenValue" 
               type="hidden" value="Initial Value" runat="server" />
        Enter a string: 
        <input id="StringContents" type="text" size="40" runat="server" />
        <br />
        <input id="Submit1" type="submit" value="Enter" 
               onserverclick="SubmitBtn_Click" runat="server" />
        <br />
        <span id="Span1" runat="server">
           This label will display the previously entered text.
        </span>
    </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
<title>HtmlInputHidden Control</title>

    <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 id="Form1" runat="server">
        <input id="HiddenValue" 
               type="hidden" value="Initial Value" runat="server" />
        Enter a string: 
        <input id="StringContents" type="text" size="40" runat="server" />
        <br />
        <input id="Submit1" type="submit" value="Enter" 
               onserverclick="SubmitBtn_Click" runat="server" />
        <br />
        <span id="Span1" runat="server">
           This label will display the previously entered text.
        </span>
    </form>
</body>
</html>

See Also

Reference

HtmlInputHidden

Other Resources

HTML Server Controls