Asp. HiddenField hidden field controls in net-basic application

Source: Internet
Author: User

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.

Copy Code code as follows:

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

Basic use of HiddenField controls

Copy Code code as follows:
<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= "click button" onclick= "Button1_Click" runat= "Server"/>
Click <asp:label id=label1 text= "0" runat=server/> Times
</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.

Ii. HiddenField Event ValueChanged

HiddenField more commonly used is the ValueChanged event, which triggers the event when the value changes. However, in actual use, you should know the page record order. In the page return process, the specific page cycle you can go to the following Web site to view
Http://msdn2.microsoft.com/zh-cn/library/ms178472.aspx

The following example illustrates the problem

Copy Code code as follows:

<script runat= "Server" language= "C #" >
protected void Page_Load (object sender, EventArgs e)
{
Response.Write ("<p> page of the Page_Load event trigger, 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 to change the value of the hidden before the event trigger, 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 valuechanged event triggering, triggering time is:" + DateTime.Now.ToString ());
}
</script>
<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>

Third, use JavaScript to pass the value to HiddenField

JavaScript directly changes the value of the control and then the background can not get the value, there is a disguised value in the HiddenField, the code is as follows:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> Untitled Page </title>
<script type= "Text/javascript" >
function SetValue () {
document.getElementById ("<%=name.") ClientID%> "). value=" AAAA ";
}
</script>
<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>

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.