The HiddenField control provides you with a way to store information in the page without displaying it. For example, you might store a user-preference setting in a HiddenField control so that it can be read in client script. To put information into a HiddenField control, you set its Value property to the value you want to store between postbacks.
You can use the HiddenField control to:
1. Store data in a hidden field on a page.
2. Detect when data stored in the hidden field has changed between postbacks.
The information in a HiddenField control is not displayed when the browser renders the page. However, it can be read and set in client script. When the page is posted back, the contents of the HiddenField control, which includes any changes made in client script, are available in server code.
It is easy for a malicious user to see and modify the contents of a hidden field. Do not store any information in a hidden field that is sensitive or that your application relies on to work properly.
Detecting Changed Data
The value of a HiddenField control can be changed before the page is posted back to the server. This might occur because you are using the hidden field to share information between server code and client script, and client script has updated the value of the control.
To help you detect changes to data in the control, the HiddenField control raises a ValueChanged event if the value of the control changes between postbacks. You can handle this event to determine whether a value has changed.
Below is the sample code for setting value for hidden field:
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
void ValueHiddenField_ValueChanged (Object sender, EventArgs e)
// Display the value of the HiddenField control.
Message.Text = "The value of the HiddenField control is " + ValueHiddenField.Value + ".";
<form id="Form1" runat="server">
Please enter a value and click the submit button.<br/>
<input type="submit" name="SubmitButton"
<asp:label id="Message" runat="server"/>
// Set the value of the HiddenField control with the
// value from the TextBox.
Form1.ValueHiddenField.value = Form1.ValueTextBox.value;