HbackField值在回发时丢失 [英] HiddenField Value Lost on Postback
问题描述
我有一些JavaScript设置 HiddenField
的值,然后强制回发。我可以跟踪这个JavaScript,它似乎正常工作。但是,当我从页面的Load事件测试 HiddenField
的值时,它不再被设置。
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.
在网上搜索,我看到很多关于丢失 HiddenField
值的帖子,但是他们似乎没有做同样的事情。
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.
这是我的JavaScript函数(已修改):
Here's my JavaScript function (modified):
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>
有没有人有任何想法?
推荐答案
如果从隐藏字段中删除 runat = server
,然后从表格参数请求中访问它,则会更容易.FORM [ EditItemId]
。然后它每次都有效。
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.
您的代码将变为:
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>
这篇关于HbackField值在回发时丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!