Hidden value assigned in js lost after postback
Asked Answered
L

3

11

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;
}
Liquate answered 7/6, 2011 at 18:45 Comment(3)
Can you show us the HTML, as it is sent to the browser? I don't know how that ASP.net stuff translates to HTML and after all, this is what JavaScript has to work with.Chafee
Are you certian that the javascript method the line you included is being called? Do you have EnableViewState set to true on the hiddenfield?Monkery
If that js code is really in a ".js file" like you say, then it's not going to work. Scriptlets (escaping to C# code) is only parsed in aspx/ascx files.Unblock
P
15

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"];
}
Prot answered 7/6, 2011 at 19:1 Comment(0)
A
5

That doesn't work like that. The value is not present since the PageLoad, so won't be postbacked. Try using a TextBox with style="display:none".

Almaraz answered 7/6, 2011 at 18:48 Comment(4)
Does it make any difference if it's a textbox or a hidden field? I tried adding this to my aspx file: <asp:TextBox ID="Test" runat="server"></asp:TextBox>, assigning a value in my js file, but had no luck.Liquate
I'm sorry, it worked! But I still don't understand why it works when using a textbox and it doesn't when using a hidden field.. Both translate to input elements..Liquate
Yes, well, TextBoxes are for user input, so they are empty at PageLoad but are posted with the new value. HiddenFields are hidden because you don't want the client to change its value, so it is preserved.Almaraz
Notice that HiddenFields are used 99% of the times to store the Id of a record that's being edited. If you coould just set a new value with javascript, imagine the security hole it would be.Almaraz
M
0

Please use

<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true"/>

Then we will get the value after postback.

All the properties of HiddenField are as bellow:

<asp:HiddenField
    EnableTheming="True|False"
    EnableViewState="True|False"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    OnValueChanged="ValueChanged event handler"
    runat="server"
    SkinID="string"
    Value="string"
    Visible="True|False"
/>
Mammon answered 27/2, 2017 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.