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

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


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.


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.