ASP.NET - what is the use of hidden field

Asked By sri on 26-Aug-11 07:20 AM
what is the use of hidden field . hidden work in the page only................?
instead of view state and cookie .
Reena Jain replied to sri on 26-Aug-11 07:23 AM
hi,

ASP.NET allows you to store information in a http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx control, which renders as a standard HTML hidden field. A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you want to store directly in the page.

A http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx control stores a single variable in its http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.value.aspx property and must be explicitly added to the page.

In order for hidden-field values to be available during page processing, you must submit the page using an HTTP POST command. If you use hidden fields and a page is processed in response to a link or an HTTP GET command, the hidden fields will not be available.

Hope this will help you

Jitendra Faye replied to sri on 26-Aug-11 07:51 AM
Hidden fields store only one value in their value property. The value is saved as a string and therefore in order to use it for other types you need to perform casting.

Hidden fields' data is submitted to the server only in HTTP post operation. You can see the stored data easily by using the View Source operation of the browser. You can see it by clicking the right mouse button and then choosing View Source from the menu (if the operation is available). Be aware not to use hidden fields to store confidential data!

The values has page context and therefore when you leave a page the data stored in the hidden fields is disposed.

Example-

<input id="HiddenField1" type="hidden" value="" runat="server" />

protected void Page_Load(object sender, EventArgs e)
{
HiddenField1.Value = "1";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(HiddenField1.Value);
}

Try this and let me know.
Riley K replied to sri on 26-Aug-11 07:57 AM

The HiddenField control is used to store a value that needs to be persisted across posts to the server. It is rendered as an element. Normally view state, session state, and cookies are used to maintain the state of a Web Forms page. However, if these methods are disabled or are not available, you can use the HiddenField control to store state values.

 To specify the value for HiddenField a control, use the Value property. You can provide a routine that gets called everytime the value of the HiddenField control changes between posts to the server by creating an event-handler for the ValueChanged event.



Example

protected void Page_Load(object sender, EventArgs e)
{
hnd1.Value = "Jayakumar";
}

Client Side


<asp:HiddenField ID="hnd1" runat ="server" />
Radhika roy replied to sri on 27-Aug-11 06:46 AM
You can use Hidden Filed like this-

aspx page page
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Show Hidden Field" />
<br />
<hr />
Clicked <asp:Label ID="Label1" runat="server"></asp:Label> times



.cs Page


Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles Button1.Click
If (HiddenField1.Value = NothingThen
HiddenField1.Value = "0"
End If

'Increment the HiddenField value by 1
HiddenField1.Value = Val(HiddenField1.Value) + 1

Label1.Text = HiddenField1.Value
End Sub




Hope  this will help you.