Javascript 从后面的代码更新时,asp.net 隐藏字段不保留值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4993406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net hidden field not retaining value when updated from code behind
提问by Ciaran Bruen
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.net 页面中存储值。基本上,每当表单上的值发生更改时,我都会设置隐藏字段的值,即名字、日期等。该字段位于具有母版页且位于内容部分的网络表单上:
<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:
每当 web 表单上的其他控件中触发 onchange 事件时,我都会通过调用函数来更改 javascript 中字段的值:
<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.
到现在为止还挺好。当页面加载时,隐藏字段值为“false”,如果我不更改 webform 上的任何值,它仍然为 false。每当我更改另一个控件时,javascript 函数都会被调用,并且隐藏字段值会更新为“true”。再一次,这一切都很好。
After I submit the form and update the database, I set the hidden field value back to 'false' in the code behind:
提交表单并更新数据库后,我在后面的代码中将隐藏字段值设置回“false”:
hdnDirtyFlag.Value = "false";
But when I click another button and do a postback, the hidden field value is still at 'true'.
但是当我单击另一个按钮并进行回发时,隐藏字段值仍为“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.
谁能解释这是为什么?我单步执行后面的代码,在更改值后立即我可以看到该值为“false”。页面上有一个 asp:UpdatePanel 但隐藏字段不是该面板的一部分。
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:
这是我用来检查第二次回发中代码后面字段值的代码,在第一次回发的最后一步中将其设置为 false 之后。由于某种原因,该值在第二次回发中保持为 true,在客户端的 javascript 中将其设置为 true 后,在后面的代码中将其设置回 false,如上所示:
if (hdnDirtyFlag.Value == "true")
{
UpdateSecurityObject();
}
回答by patmortech
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.
您的问题是您的隐藏字段在更新面板之外。即使更新面板在回发期间可以访问页面上的所有控件(因为它的行为类似于正常的回发),但它不会更新页面客户端上位于 ContentTemplate 之外的任何控件。因此,您在代码隐藏中更改隐藏字段值的代码不会对客户端的值产生影响。这就是为什么第二次单击按钮时它仍然设置为 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.
您将需要 a) 将隐藏字段放在 UpdatePanel 中(或者您可以将它放在自己的面板中,并将 UpdateMode 设置为 Always);或 b) 在客户端有一些 javascript,当 UpdatePanel 调用完成将值设置回 false 时会触发。
回答by 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.
ViewState 正在持久化该值,因此当页面重新加载时,ViewState 中包含 true,因此 asp.net 在页面呈现之前将控件的值更新为 true。
Change your HiddenField to this:
将您的 HiddenField 更改为:
<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.
这将阻止 asp.net 跨回发维护此字段的值,因为您的目的是在每次页面加载时将其设置为 false。