admin管理员组

文章数量:1356808

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.

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.

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>

Does anyone have any ideas?

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.

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.

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>

Does anyone have any ideas?

Share Improve this question edited Jun 14, 2011 at 19:55 Jonathan Wood asked Jun 14, 2011 at 19:45 Jonathan WoodJonathan Wood 67.4k82 gold badges304 silver badges531 bronze badges 2
  • Jonathan. Did you get it working in the end? – marto Commented Jun 20, 2011 at 9:16
  • @marto: Not quite. It appears to have something to do with the dynamically loaded user control. I was actually able to better define the problem and even reproduce the issue in a small test project. I've posted a new question here. – Jonathan Wood Commented Jun 20, 2011 at 15:10
Add a ment  | 

2 Answers 2

Reset to default 6

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.

Your code will bee:

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>

If you're expecting the value upon an AJAX post-back via the UpdatePanel then you need to put it inside the ContentTemplate...

本文标签: javascriptHiddenField Value Lost on PostbackStack Overflow