3

Am facing a very strange issue. I've a hidden fields as shown below.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
        <script src="http://code.jquery.com/jquery-1.9.0.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:HiddenField ID="hid_test" ClientIDMode="Static" runat='server' />
    </div>
    </form>
<script language="javascript" type="text/javascript">
    $(function () {
        alert($('#hid_test').val());
    });
</script>    
</body>

</html>

And in server side am setting a value to the hidden field as follows

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        hid_test.Value = "abcd"
    End If
End Sub

Very simple code. So in the first run the alert shows as "abcd" since am setting that from server side. OK.. Thats fine.

Then what I did was I changed hidden field value using jquery from console as below

$('#hid_test').val('12');

After this change does when I hit F5 (Page reloads) obviously my server side code hits and the hidden fields value should be changed to abcd

But when the page loads the alert says 12 itself. Means its keeping the value set from client side. Any kind help appreciated. Am testing in Firefox.

I disabled FF form fill feature as follows enter image description here

Sandeep Thomas
  • 4,303
  • 14
  • 61
  • 132

2 Answers2

3

Firefox auto-fills forms with previous values by default. If you turn off the feature, you should stop seeing that behavior. This question and its answers seem to suggest there's no simple way to tell Firefox not to do that in the HTML; instead, the answers there focus on using JavaScript on load/unload to set the field values (e.g., on load to overwrite what Firefox auto-filled; on unload to encourage Firefox to remember the values the page author wants remembered).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • So FF was the villain?? It took my 2 days.. Thanks mate.. But just an interest to know.. thats really a wrong feature in a browser... right? Because if any server side changes being replaced by browser sounds totally wrong.. – Sandeep Thomas May 01 '15 at 07:36
  • @sforsandeep: Yup, this is a common issue with Firefox. If you [turn it off](https://support.mozilla.org/en-US/kb/control-whether-firefox-automatically-fills-forms) you should be able to verify that (since, you know, I could be wrong :-) ). – T.J. Crowder May 01 '15 at 07:37
  • @sforsandeep: Not necessarily a bug; it's a feature that sometimes gets in the way of what we want to do. The idea is that it helps you by remembering things you've filled in before, so you don't have to repetitively do that in the future. Overwriting what comes from the server is fundamental to that feature. – T.J. Crowder May 01 '15 at 07:39
  • I disabled that feature (Sorry if I did that wrongly) and the problem still persist.. Please check my edit in Question. But in chrome the problem not there.. – Sandeep Thomas May 01 '15 at 07:47
  • @sforsandeep: That tells it not to store new entries. You also need to clear the existing entries. See "Clearing form history" [here](https://support.mozilla.org/en-US/kb/control-whether-firefox-automatically-fills-forms). – T.J. Crowder May 01 '15 at 07:49
  • @sforsandeep: Good deal. I just wish there were a way for us to mark a field to say "No, really, hand's off -- we control the default value here." :-) – T.J. Crowder May 01 '15 at 09:55
  • Exactly.. Something about your second point in the answer.. If we clear the hidden field in 'beforeunload' event, firefox did a wrong thing it clears its form. But when any data comes from server, it replaces the data with its "Empty" in cache. That means the hidden field will be blank! – Sandeep Thomas May 01 '15 at 10:44
  • "Not necessarily a bug; it's a feature that sometimes gets in the way of what we want to do." They do it for hidden fields, so it's a sloppy feature/bug. – Dtipson Dec 22 '15 at 18:12
1

Add the autocomplete="off" tag.

space_food_
  • 800
  • 1
  • 7
  • 23