asp.net隐藏字段从code更新时,后面不保留价值 [英] asp.net hidden field not retaining value when updated from code behind

查看:183
本文介绍了asp.net隐藏字段从code更新时,后面不保留价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个隐藏字段存储在一个asp.net页面的值。基本上我设置隐藏字段的值,只要在窗体上的值改变,即第一个名字,日期等领域是对具有母版页,并在内容部分一个WebForm:

 < ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
    < ASP:HiddenField ID =hdnDirtyFlag=服务器VALUE =假/>

我通过调用一个函数更改JavaScript中的字段的值每当onchange事件火灾其他控件的Web表单上:

 < ASP:文本框的CssClass =niceInputID =tbFirstName=服务器MAXLENGTH =40WIDTH =150的onchange ='SetHiddenVariable();' >< / ASP:文本框> <脚本类型=文/ JavaScript的>
     功能SetHiddenVariable(){
         //设置隐变量的值,所以我们知道一个字段已更新
         VAR hiddenControl ='<%= hdnDirtyFlag.ClientID%GT;';
         的document.getElementById(hiddenControl).value的=真;
     }
< / SCRIPT>

到目前为止好。当页面加载的隐藏字段的值是'假',如果我不改变对Web窗体的任何值仍然为false。每当我做更改另一个控制的JavaScript函数被调用和隐藏字段的值被更新为真。再次,这是所有罚款。

我提交表单,并更新数据库后,我在后面的code设置隐藏字段的值回'假':

  hdnDirtyFlag.Value =假;

但是,当我点击另一个按钮,做回传,隐藏字段的值仍处于真。

谁能解释这是为什么?我从背后code和改变的价值,我可以看到的值是'假'后,立即上前。有一个asp:页面,但隐藏字段上的UpdatePanel不是这个小组的一部分

编辑:

这是code我用它来检查code中的字段的值暂列第二回传后,它已在第一回发的最后一步被设置为false。该值保持在真正出于某种原因在第二回传后,它已在客户端的JavaScript被设置为true,然后重新设置为false code如上图所示背后:

 如果(hdnDirtyFlag.Value ==真)
{
    UpdateSecurityObject();
}


解决方案

您的问题是,你的隐藏字段更新面板之外。即使一个更新面板具有回发期间访问所有的控件在页面上(因为它就像一个正常的回发),它不更新是的ContentTemplate之外页面客户端的任何控件。因此,在这种情况正在改变隐藏字段的值codebehind您code是不是有对客户端的价值产生影响。这就是为什么你第二次单击该按钮,它仍设置为true。

您将需要或者a)把隐藏字段的UpdatePanel内(或者你可以把它放在自己的面板用的UpdateMode设置为Always);或b)对客户端的JavaScript的一些时,将触发设定值回假的UpdatePanel调用完成。

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(); 
}

解决方案

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.

这篇关于asp.net隐藏字段从code更新时,后面不保留价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆