Use HiddenField control in asp.net 2.0

Source: Internet
Author: User

The HiddenField control, as its name implies, is a server control that hides the input box, allowing you to save data that is not required to be displayed on the page and that is less secure. Perhaps this time should have such a question, why have the ViewState, session and cookie State preservation mechanism, still need to use HiddenField?

To increase the HiddenField, in fact, to make the whole state management mechanism more comprehensive application. Because whether it is viewstate, cookies or session, there is a time when it is invalid, such as the user because of a requirement to set ViewState to false, or environmental conditions to restrict the use of cookies, or the user for a long time does not act cause session expiration and so on , this time HiddenField is undoubtedly the best choice.

The function of the HiddenField control is simply to store the value that needs to be persisted between sending to the server. It is rendered as a <input type= "hidden"/> element, and can be made a standard HTML server control by adding runat= "Server". The properties and events that asp.net HiddenField Web server controls can use 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 the HiddenField is rendered to the client browser, it does not apply to storing security-sensitive values. To specify a value for the HiddenField control, use the Value property, and note that it is value rather than text. In fact, HiddenField does not have the Text property, which is consistent with the property naming of standard buttons such as DropDownList, CheckBoxList, and so on. In the standard attribute naming method, the value of text is presented to the user, and value is controlled by the code. For example, you can have the DropDownList Text property display the username and let its value store the user's number.

The following code shows the basic use of a modified control.



<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>

<body>
<form runat=server>
<asp:HiddenField id=HiddenField1 runat=Server />
<asp:Button id=Button1 Text="单击按钮" onclick="Button1_Click" runat="server" />
单击 <asp:Label id=Label1 Text="0" runat=server /> 次
</form>
</body>

In the code above, <asp:hiddenfield id=hiddenfield1 runat=server/> Defines a hidden control that calculates the number of times a user clicks in a button's Click event and assigns the number of changes to Label1.

You can change the <asp:hiddenfield id=hiddenfield1 runat=server/> In the previous code to <input Type=hidden id=hiddenfield1 runat=Server > is OK.

In the code above, if you view the source from the browser, you will get the following information:

<form name="Form1" method="post" action="Default.aspx" id="Form1">

This is because HiddenField is passing data through the HTTP protocol, so HiddenField is not available if you open a new form page through "method=" or a link.

In addition, HiddenField does not replace the session to maintain state, in the above example, although you click a button to show the number of clicks but it does not mean that it can record your status information. If you reopen the browser then you see that this is still 0 instead of 3.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.