ASP.NET Hidden fields data only available in a postback? why?

1.3k views Asked by At

According to MSDN hidden fields section,

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.

If I add a HiddenField control at design time and set a value in it at design time or in the Init event in ASP.NET, why would I not be able to read/process the value when a page is first requested?

1

There are 1 answers

0
Pradeep On

How have you defined your hidden field?

You need to make hidden field as runat="server" like this:

<input id="something" type="hidden" value="something that is hidden" runat="server" />

Then you will be able to access this field on server even if it is Get request.

protected void Page_Load(object sender, EventArgs e)
{
     Response.Write(string.Format("{0} came from hidden field", something.Value));
}