asp.net hidden field not retaining value when updated from code behind

Ciarán Bruen picture Ciarán Bruen · Feb 14, 2011 · Viewed 76k times · Source

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:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:HiddenField ID="hdnDirtyFlag" runat="server" Value='false' />

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:

<asp:TextBox CssClass="niceInput" ID="tbFirstName" runat="server" MaxLength="40" Width="150" onchange='SetHiddenVariable();'></asp:TextBox>

 <script type="text/javascript">
     function SetHiddenVariable() {
         // Set the value of the hidden variable so we know a field has been updated
         var hiddenControl = '<%= hdnDirtyFlag.ClientID %>';
         document.getElementById(hiddenControl).value = 'true';
     }
</script>

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.

After I submit the form and update the database, I set the hidden field value back to 'false' in the code behind:

hdnDirtyFlag.Value = "false";

But when I click another button and do a postback, the hidden field value is still at 'true'.

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.

EDIT:

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:

if (hdnDirtyFlag.Value == "true")
{
    UpdateSecurityObject(); 
}

Answer

patmortech picture patmortech · Feb 16, 2011

Your problem is that your hidden field is outside the update panel. Even though an update panel has access to all controls on a page during postback (since it acts like a normal postback), it does NOT update any controls on the page client-side that are outside of the ContentTemplate. So your code in the codebehind that is changing the value of the hidden field is not having an effect on the value on the client side. That's why the second time you click the button it is still set to true.

You will need to either a) put the hidden field inside the UpdatePanel (or you could put it in its own panel with UpdateMode set to Always); or b) have some javascript on client-side that fires when the UpdatePanel call completes that sets the value back to false.