title: asp.net hidden field not retaining value when updated from code behind
body: <p>I'm using a hidden field to store a value in an asp.net page. Basically I set the value of the hidden field whenever a value on the form is changed i.e. first name, date etc. The field is on a webform that has a master page and is in the content section:</p>
  
  <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;
      &lt;asp:HiddenField ID="hdnDirtyFlag" runat="server" Value='false' /&gt;
  </code></pre>
  
  <p>I change the value of the field in javascript by calling a function whenever an onchange event fires in other controls on the web form:</p>
  
  <pre><code>&lt;asp:TextBox CssClass="niceInput" ID="tbFirstName" runat="server" MaxLength="40" Width="150" onchange='SetHiddenVariable();'&gt;&lt;/asp:TextBox&gt;
  
   &lt;script type="text/javascript"&gt;
       function SetHiddenVariable() {
           // Set the value of the hidden variable so we know a field has been updated
           var hiddenControl = '&lt;%= hdnDirtyFlag.ClientID %&gt;';
           document.getElementById(hiddenControl).value = 'true';
       }
  &lt;/script&gt;
  </code></pre>
  
  <p>So far so good. When the page loads the hidden field value is 'false', and if I don't change any values on the webform it remains false. Whenever I do change another control the javascript function gets called and the hidden field value gets updated to 'true'. Again this is all fine.  </p>
  
  <p>After I submit the form and update the database, I set the hidden field value back to 'false' in the code behind: </p>
  
  <pre><code>hdnDirtyFlag.Value = "false";
  </code></pre>
  
  <p>But when I click another button and do a postback, the hidden field value is still at 'true'. </p>
  
  <p>Can anyone explain why this is? I stepped through the code behind and immediately after changing the value I can see the value is 'false'. There is an asp:UpdatePanel on the page but the hidden field is not part of this panel.</p>
  
  <p>EDIT:</p>
  
  <p>This is the code I use to check the value of the field in code behind  in the second postback, after it has been set to false in the last step of the first postback. The value remains at true for some reason in the second postback, after it has been set to true in javascript on the client side then set back to false in code behind as shown above:</p>
  
  <pre><code>if (hdnDirtyFlag.Value == "true")
  {
      UpdateSecurityObject(); 
  }
  </code></pre>