How to use HiddenField hidden domain control in ASP. NET

  • 2021-07-13 05:04:46
  • OfStack

The purpose of the HiddenField control is simply to store values that need to be persisted between transmissions to the server. It acts as a < input type= "hidden"/ > Element and makes it a standard HTML server control by adding runat = "server". The properties and events that can be used by ASP. NET HiddenField Web server controls are listed below.


<asp:HiddenField
    EnableTheming="True|False"
    EnableViewState="True|False"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    OnValueChanged="ValueChanged event handler"
    runat="server"
    SkinID="string"
    Value="string"
    Visible="True|False"
/>

Because the value of HiddenField will be rendered to the client browser, it is not suitable for storing security-sensitive values. To specify a value for an HiddenField control, use the Value property, noting that it is Value instead of Text. In fact, HiddenField does not have Text attribute, which is similar to the attribute naming method of standard buttons such as DropDownList and CheckBoxList. In standard attribute naming, the value of Text is presented to the user, while the value of Value is controlled by code. For example, you can have the Text property of DropDownList display the user name and its Value store the user number.

1. Basic use of the HiddenField control

<html> 
    <head>
        <script language="C#" runat="server">
        void Button1_Click(object sender, EventArgs e)
        {
            if (HiddenField1.Value == String.Empty)
                HiddenField1.Value = "0";
            HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value)+1).ToString();
            Label1.Text = HiddenField1.Value;
        }
        </script>
    </head>
    <body>
        <h3><font face="Verdana">HiddenField</font></h3>
        <form runat=server>
            <asp:HiddenField id=HiddenField1 runat=Server />
            <asp:Button id=Button1 Text=" Click a button " onclick="Button1_Click" runat="server" />
            Click <asp:Label id=Label1 Text="0" runat=server /> Times
        </form>
    </body>
</html>

In the above code, < asp:HiddenField id=HiddenField1 runat=Server / > A hidden control is defined to calculate the number of clicks by the user in the click event of the button, and assign the number of clicks to Label1.

You can change the < asp:HiddenField id=HiddenField1 runat=Server / > Replace with < input type=hidden id=HiddenField1 runat=Server > It is also possible

In using the above code, if you look at the source code from the browser, you will get the following information:
< form name="Form1" method="post" action="Default.aspx" id="Form1" >
This is because HiddenField passes data through the HTTP protocol, so if you open a new form page via "method=" get "or a link, HiddenField is not available.
In addition, HiddenField does not replace Session to maintain status. In the above example, although you click the button once, it can display the number of clicks, but it does not mean that it can record your status information. If you reopen the browser, what you see here is still 0 instead of 3.

2. HiddenField Event ValueChanged

More commonly used for HiddenField is the ValueChanged event, which triggers when the Value value changes. However, in actual use, you should know the order of page records. In the process of page return, you can check the specific page cycle at the following website
http://msdn2.microsoft.com/zh-cn/library/ms178472.aspx

The following example illustrates this problem


<html>
<head>
<script runat="server" language="c#">
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("<p> Page's Page_Load Event is triggered by: " + DateTime.Now.ToString());
    if (HiddenField1.Value == String.Empty)
    HiddenField1.Value = "0";
}
protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("<p>Button1_Click For change Hidden The event is triggered before the value of, and the trigger time is: " + DateTime.Now.ToString());
    HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString();
    Label1.Text = HiddenField1.Value;
}
protected void HiddenField1_ValueChanged(object sender, EventArgs e)
{
    Response.Write("<p>HiddenField Adj. ValueChanged Event is triggered by: " + DateTime.Now.ToString());
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:HiddenField ID="HiddenField1" runat="server" OnValueChanged="HiddenField1_ValueChanged" />
</div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form></body>
</html>

3. Pass the value to HiddenField using javascript

javascript directly changes the value of the control and can't get the value in the background. There is a disguised value in HiddenField. The code is as follows:


<!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 setValue(){
        document.getElementById("<%=name.ClientID %>").value="aaaa";
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:HiddenField ID="name" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button"
            OnClientClick="setValue()" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>


Related articles: