The HiddenField control, as its name suggests, is a server control that hides input boxes. It allows you to save data that does not need to be displayed on the page and does not require high security. Maybe there should be a question at this time, why do we still need to use HiddenField when we have state saving mechanisms such as ViewState, Session, and Cookie?
The purpose of adding HiddenField is to make the application of the entire state management mechanism more comprehensive. Because whether it is ViewState, Cookie or Session, there will be a time when it expires. For example, the user requires to set ViewState to false due to certain needs, or environmental conditions restrict the use of Cookie, or the user has been inactive for a long time causing the Session to expire, etc. At this time, HiddenField is undoubtedly the best choice.
The function of the HiddenField control is simply to store the values that need to be maintained between sending to the server. It is rendered as an <input type="hidden"/> element, and can be made into a standard HTML server control by adding runat="server". Listed below are the properties and events available for the ASP.NET HiddenField web server control.
<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 HiddenField's value will be rendered to the client browser, it is not suitable for storing security-sensitive values. To specify a value for the HiddenField control, use the Value property. Note that it is Value and not Text. In fact, HiddenField does not have a Text property, which is consistent with the property naming of standard buttons such as DropDownList and CheckBoxList. In the standard property naming method, the value of Text is presented to the user, while the value of Value is controlled through code. For example, you can have the DropDownList's Text property display the user's name and its Value store the user's number.
The code below shows the basic use of the 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 button" onclick="Button1_Click" runat="server" />
Clicked <asp:Label id=Label1 Text="0" runat=server /> times
</form>
</body>
</html>
In the above code, <asp:HiddenField id=HiddenField1 runat=Server /> defines a hidden control that counts the number of user clicks in the button's click event, and assigns the number of changes to Label1.
You can change <asp:HiddenField id=HiddenField1 runat=Server /> in the above code to <input type=hidden id=HiddenField1 runat=Server>. It is also possible
to use the above code. If you view the source from the browser The code will get the following information:
<form name="Form1" method="post" action="Default.aspx" id="Form1">
This is because HiddenField transmits data through the HTTP protocol, so if you open a new form page through "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 to display the number of clicks, it does not mean that it can record your status information. If you reopen the browser you will still see 0 here instead of 3.
HiddenField event
The more commonly used HiddenField is the ValueChanged event, which is triggered when the Value changes. However, in actual use, you must know the order of page recording. During the page postback process, you can check the specific page cycle on the following website
http://msdn2.microsoft.com/zh-cn/library/ms178472.aspx
The following example illustrates this problem
<head>
<script runat="server" language="c#">
protected void Page_Load(object sender, EventArgs e)
{ Response.Write("<p>The Page_Load event of the page is triggered, and the trigger time is: " + DateTime.Now.ToString());
if (HiddenField1.Value == String.Empty)
HiddenField1.Value = "0"; }
protected void Button1_Click(object sender, EventArgs e)
{ Response.Write("<p>Button1_Click is an event triggered before changing the value of Hidden. 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's ValueChanged event is triggered, and the trigger time is: " + 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>
In this example, the result we want is: when the user clicks the button, the Value of HiddenField1 is changed through the button's Button1_Click event, and then the HiddenField1_ValueChanged event of HiddenField1 is triggered. However, is this really the case?
Run the above code and get the result. As you can see, the Button does change the value of the HiddenField every time it is clicked, but the output we defined in HiddenField1_ValueChanged is not executed. In other words, the page does not execute the ValueChanged event. To understand this problem, you also need to understand the declaration cycle of the page. During the page cycle, you can see that the control properties are read or initialized in Page_Init, and then Control events.
The event here means: In the Page_Init event, the web page will accept the data returned by the user, for example, assign <span id="Label1">Label</span> to the Text attribute with ID Label1, and <input type The value of ="hidden" name="HiddenField1" id="HiddenField1" value="0" /> is assigned to the Value property of HiddenField1. After all initialization is completed, the page begins to execute the control event - Button1_Click, and changes the HiddenField's Value in the Button event. So since the Value has been changed here, why is the ValueChanged event not executed?
At this time, although the Value value has been changed here, it is saved in Page_Init. This is because when the Button button is clicked, although the HiddenField is changed, the reply of the page is also triggered again, that is, although the previous HiddenValue The value is 0, and this time its value is changed to 1. However, after the page is posted back, since ViewState will save the last installation (here is 1), so in Page_Init, the initial value of HiddenField is considered to be 1, and this time The times are still 1, which makes it feel that the data has not changed, so the ValueChanged event will still not be triggered.
Of course, you can disable HiddenField for processing, and you can execute the ValueChanged event, but in fact after you disable ViewState, the page no longer saves the value of ViewState, so The page thinks that each request for HiddenField is new, such as the following code:
You didn't change the Hiddenfield value, but it still executes every time.
<%@ Page EnableViewState="false" %>
<head>
<script runat="server" language="c#">
protected void Page_Load(object sender, EventArgs e)
{
if (HiddenField1.Value == String.Empty)
HiddenField1.Value = "111";
}
protected void Button1_Click(object sender, EventArgs e)
{ // HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString();
Label1.Text = TextBox1.Text; }
protected void HiddenField1_ValueChanged(object sender, EventArgs e)
{ Response.Write("Changed." + DateTime.Now.ToString());
Response.Write(HiddenField1.Value);
Response.Write(TextBox1.Text); }
</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:TextBox runat=server ID=TextBox1></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body >
</html>
Using HiddenFile combined with the "Cross Page" page provided by ASP.NET2.0 can realize the transfer of page data. This situation is for such a solution:
In a registration page, the user needs to enter data. Since there may be a lot of data in the remarks column, a control similar to FreeTextBox can be used in a new window to allow the user to format the text, and after the input is completed, return to the original registration page. Regarding this situation, we will introduce it later