23

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(); 
}
Ciarán Bruen
  • 5,221
  • 13
  • 59
  • 69
  • When you look at the source of the page in your browser when it loads after a postback, is the value of the hidden field still 'true'? The way you phrase it above, you are seeing 'true' after you postback a second time, but don't say whether you check to see that it actually was still 'true' before you do anything on the page. – patmortech Feb 14 '11 at 14:51
  • Hi @patmortech in the source the value is false both when the page loads originally and after a postback. After I set the value of the field to 'false' in code behind, I'm seeing 'true' the next time I post back. – Ciarán Bruen Feb 14 '11 at 15:12
  • Are you using Firebug? If so, put a breakpoint in your SetHiddenVariable function. Check whether it's getting called when you don't expect it to. – patmortech Feb 14 '11 at 15:23
  • Not using Firebug using IE8. I added an alert('blah') to SetHiddenVariable and it only gets called when I expect it i.e. after changing a value in a control with the 'onchange' event. Will Firebug break when there's a breakpoint in VS javascript code? – Ciarán Bruen Feb 14 '11 at 15:32
  • Firebug doesn't care where your javascript comes from -- you just have to find your method in its script tab and set a breakpoint. – patmortech Feb 14 '11 at 16:05
  • So you are saying that on the SECOND postback (where you didn't change the textbox value), your SetHiddenVariable does NOT fire, yet the value of the hidden field is somehow set to True when you check it on the server? – patmortech Feb 14 '11 at 16:07
  • @patmortech that's correct, I set the hidden field to 'false' as the last step in the first postback, but the value of the hidden field is 'true' on the next postback even though SetHiddenVariable doesn't fire. – Ciarán Bruen Feb 14 '11 at 16:24
  • Can you show us the code where you are checking the value of the checkbox? Or you treating it as a string or a boolean? – Mark At Ramp51 Feb 14 '11 at 21:56
  • Hi @Mark I don't check a check box value maybe you meant hidden field? Added some more code to the question above if that helps..thnx – Ciarán Bruen Feb 15 '11 at 08:34
  • Well, I created a simple page with only the controls/code you mention above, and it works as expected. I think we would have to see your full code-behind and aspx page to figure out what the error is. Maybe use codepaste.net to show us (rather than pasting into your question). – patmortech Feb 15 '11 at 15:10
  • Yeah, I misspoke about the checkbox I meant hidden field. I agree with patmoretech, I'm about out of ideas given the current information. i am also a little confused about what role the update panel is playing in your page. You said the hidden field isn't part of the update panel, but what part of the page is? – Mark At Ramp51 Feb 15 '11 at 15:41
  • Well I read something about hidden fields not working when they're on Update panels. Most of the code is on an Update panel so I put the hidden field outside the panel. I'll look at getting the code up on codepaste.net..thanks for the help so far – Ciarán Bruen Feb 15 '11 at 17:01
  • Chaps got the code up codepaste. I didn't trim it just lashed the whole lot up there. It's basically a crud form with paging that retrieves and updates records from a WCF service. aspx code here : http://www.codepaste.net/13jonp, code behind here: http://www.codepaste.net/rwdug2 – Ciarán Bruen Feb 15 '11 at 23:04

2 Answers2

48

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.

patmortech
  • 10,139
  • 5
  • 38
  • 50
5

ViewState is persisting the value, so when the page reloads the ViewState has true in it, so asp.net updates the value of the control with true before the page renders.

Change your HiddenField to this:

<asp:HiddenField ID="hdnDirtyFlag" runat="server" Value='false' EnableViewState="false" />

This will prevent asp.net from maintaining the value of this field across postbacks, since your intention is to have it set to false each time the page loads.

Dov Miller
  • 1,958
  • 5
  • 34
  • 46
Mark At Ramp51
  • 5,343
  • 1
  • 25
  • 30
  • 1
    Mark I thought you cracked it but after changing to what you suggest and running again it's still the same :( – Ciarán Bruen Feb 14 '11 at 14:58
  • All inputs dont use viewstate for the value property. It uses the http value that is sent in the Request.Form list. Disabling ViewState in hidden field is kind of useless since the only property that matter is the Value and it doesn't even use the ViewState. – Kat Lim Ruiz Oct 01 '14 at 16:51