Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden value assigned in js lost after postback

Here's my problem. I have a hidden field whose value I change through a javascript method. The problem is after postback the value is lost.

How can I persist the value after postback?

Thanks!

.aspx File

<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="BtnGuardar" runat="server" OnClick="BtnGuardar_Click" OnClientClick="return GridUpdateInfoOK()" />

.js file

document.getElementById('<%= HiddenField1.ClientID %>').value = 'TEST';

.aspx.cs file

protected void BtnGuardar_Click(object sender, EventArgs e)
{
    String test = HiddenField1.Value;
}
like image 711
Gonzalo Avatar asked Jun 07 '11 18:06

Gonzalo


People also ask

How do you persist the hidden field value after PostBack?

Inside the Page Load event of the ASP.Net Page, the value of the Hidden Field set using JavaScript is fetched from the Request. Form collection using the UniqueID property. The fetched value is again set to the Label control, this makes the Label retain its value on PostBack.

What is hidden field in JavaScript?

Definition and Usage. The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.


1 Answers

You don't need to have the hidden input run at server. You can do:

<input type="hidden" id="HiddenInput" name="HiddenInput" value="" />

Then when you post back you can access it like that:

protected void BtnGuardar_Click(object sender, EventArgs e)
{
    String test = Request.Form["HiddenInput"];
}
like image 183
marto Avatar answered Oct 17 '22 19:10

marto