Asp.net| Control
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"
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= "click button" 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.
HiddenField Events
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
<script runat= "Server" language= "C #" >
protected void Page_Load (object sender, EventArgs e)
{Response.Write ("<p> page 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 hidden value before the event is triggered, 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 Trigger, the trigger 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" text= "button"/>
</form></body>
In this example, we want the result: when the user clicks the button, the Button1_Click event of the button changes the value of HiddenField1 and then triggers the HiddenField1 hiddenfield1_ ValueChanged events, but is it really so?
Run the above code and get the result as you can see, the button does change the value of the HiddenField each time you click, but the output we defined in hiddenfield1_valuechanged is not executed. In other words, the page does not perform valuechanged events
To understand this problem, you also need to understand the page's declaration cycle, and during the page cycle you can see that the control properties are read or initialized in the Page_Init, and then the controls events.
The event here means that in the Page_Init event, the Web page accepts data returned by the user, such as <span id= "Label1" >Label</span> assigned to the Text property with the ID Label1, < The value value of the input type= "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's event--button1_click, changing the value of the HiddenField in the button event. So now that you've changed the value, why didn't you execute the valuechanged event?
At this point, while the value value has been changed here, it is saved in Page_Init because the button is currently clicked, although it changes HiddenField but again triggers the response of the page, even though the value of the previous hiddenvalue is 0, And this time to change its value to 1, but after the page return, because ViewState will save the last install (here is 1), so in Page_Init, think HiddenField initial value of 1, and this is 1, make it feel the data unchanged, So it still doesn't trigger the ValueChanged event.
Of course, you can disable HiddenField to do processing, you can perform valuechanged events, but in fact, after you disable ViewState, the page no longer save ViewState value so that the page that every request HiddenField is new, For example, the following code:
You haven't changed the value of the HiddenField, but you're still doing it every time.
<%@ Page enableviewstate= "false"%>
<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>
<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" text= "button"/>
</form>
</body>
The use of Hiddenfile and the "Cross Cross Page" page provided by asp.net2.0 enables the delivery of page data, which is for a scenario that:
In a registration page, users are required to enter data, because the memo column may be a lot of data, you can in the new window can use similar Freetextbox control to let users
Format the text and return to the original registration page after the input is complete. In this case, we will introduce