javascript HiddenField 值在回发时丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6349155/
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
HiddenField Value Lost on Postback
提问by Jonathan Wood
I have some JavaScript that sets the value of a HiddenField
and then forces a postback. I can trace through this JavaScript and it appears to work correctly. However, when I test the value of the HiddenField
from the page's Load event, it is no longer set.
我有一些 JavaScript 可以设置 a 的值,HiddenField
然后强制回发。我可以跟踪这个 JavaScript,它似乎可以正常工作。但是,当我HiddenField
从页面的 Load 事件中测试 的值时,它不再被设置。
Searching the web, I see a lot of posts about losing HiddenField
values but none of them seemed to be doing the same thing that I am.
在网上搜索,我看到很多关于失去HiddenField
价值的帖子,但似乎没有一个和我在做同样的事情。
Here's my JavaScript function (modified):
这是我的 JavaScript 函数(已修改):
function EditItemItem(itemId) {
document.getElementById('<%= EditItemId.ClientID %>').value = itemId;
__doPostBack('<%= EditItemUpdatePanel.ClientID %>', '');
}
And here's part of my markup (modified):
这是我标记的一部分(已修改):
<div id="EditItemBox" runat="server">
<asp:HiddenField runat="server" id="EditItemId" />
<asp:UpdatePanel ID="EditItemUpdatePanel" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="EditItemPanel" runat="server"
CssClass="ModalDialog" style="display:none;">
<div>Edit an Item</div>
<!-- ... -->
</asp:Panel>
</asp:UpdatePanel>
</div>
Does anyone have any ideas?
有没有人有任何想法?
回答by marto
It's easier if you remove runat=server
from the hidden field and then access it from the Form paramaters Request.Form["EditItemId"]
. Then it works every time.
如果您runat=server
从隐藏字段中删除然后从 Form 参数中访问它会更容易Request.Form["EditItemId"]
。然后它每次都有效。
Your code will become:
您的代码将变为:
function EditItemItem(itemId) {
document.getElementById('EditItemId').value = itemId;
__doPostBack('<%= EditItemUpdatePanel.ClientID %>', '');
}
<div id="EditItemBox" runat="server">
<input type="hidden" id="EditItemId" name="EditItemId" value="" />
<asp:UpdatePanel ID="EditItemUpdatePanel" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="EditItemPanel" runat="server"
CssClass="ModalDialog" style="display:none;">
<div>Edit an Item</div>
<!-- ... -->
</asp:Panel>
</asp:UpdatePanel>
</div>
回答by Kieron
If you're expecting the value upon an AJAX post-back via the UpdatePanel
then you need to put it inside the ContentTemplate
...
如果您期望通过 AJAX 回传的值,UpdatePanel
那么您需要将其放入ContentTemplate
...