从后面的代码更新时,asp.net隐藏字段不保留值

时间:2022-08-25 21:44:39

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:

每当onchange事件在Web表单上的其他控件中触发时,我通过调用函数来更改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后,在第二次回发中检查后面代码中的字段值的代码。由于某种原因,在第二次回发中,值在客户端的javascript中设置为true,然后在代码后面设置为false,如上所示:

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

2 个解决方案

#1


38  

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)在UpdatePanel调用完成时触发客户端的一些javascript,将值设置为false。

#2


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.

ViewState是持久化的值,所以当页面重新加载时,ViewState中有true,因此asp.net在页面呈现之前用true更新控件的值。

Change your textbox 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.

这将阻止asp.net在回发之间维护此字段的值,因为您的意图是每次页面加载时将其设置为false。

#1


38  

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)在UpdatePanel调用完成时触发客户端的一些javascript,将值设置为false。

#2


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.

ViewState是持久化的值,所以当页面重新加载时,ViewState中有true,因此asp.net在页面呈现之前用true更新控件的值。

Change your textbox 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.

这将阻止asp.net在回发之间维护此字段的值,因为您的意图是每次页面加载时将其设置为false。