Working with ASP.Net HiddenField Control

Updated on 05 Jul 2012,
Published on 06 Nov 2009

The ASP.Net HiddenField Control enables you to store the page specific information that you don't want to display on the web page. It stores the data in hidden mode and persists its availability even across the postbacks to the server. The hiddenfield renders as HTML input hidden type field in the browser that does not appear on the page but allows getting or setting its "Value" property through C# server code. It works only if the page is submitted using HTTP POST method. When a web page is submitted to the server it posts the value of hidden field control in the HTTP form collection along with data of other controls placed in the web form.

The HiddenField control stores the data at client side which is best to hold the frequently changed data items at the client side. It does not support rich data items to store large information. As it renders at client side to store the information it is recommended only to store small amount of information otherwise it will slow down the page loading and postback duration to send and retrieve the information back.

HiddenField Control Examples:

You can see the live samples and examples of HiddenField Control from the following links:

Sample Code for ASP.Net HiddenField Control

HTML Code
<asp:Label ID="Label1" runat="server">
</asp:Label>

<asp:Button ID="Button1" 
    runat="server" 
    Text="Click It" 
    OnClick="Button1_Click" />

<asp:HiddenField ID="HiddenField1" 
    runat="server" 
    Value="0" />
C# Code
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
}
protected void Button1_Click(object sender, EventArgs e)
{
    HiddenField1.Value = (Convert.ToInt32(HiddenField1.Value) + 1).ToString();

    Label1.Text = string.Format("Clicked {0} times", HiddenField1.Value);
}

In the above sample code we have used the hiddenfield control to store the click count of button control. When user will hit the button it will execute the server click handler that will increment the value stored in the ASP.Net HiddenField control.

Output:

Get the output for above discussed code from the following link:

HiddenField Control

Continue to the next tutorial: ASP.Net HiddenField OnValueChanged Event to learn how to detect whether the data stored in the hidden field changed or not while postback event.

0 Responses to "Working with ASP.Net HiddenField Control"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email