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

半腔热情 提交于 2019-11-30 08:09:29

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.

Mark At Ramp51

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!