1

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?

Jonathan Wood
  • 59,750
  • 65
  • 229
  • 380
  • Jonathan. Did you get it working in the end? – marto Jun 20 '11 at 09: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](http://stackoverflow.com/questions/6406180/javascript-triggered-updatepanel-not-updating-correctly). – Jonathan Wood Jun 20 '11 at 15:10

2 Answers2

5

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 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>
Community
  • 1
  • 1
marto
  • 4,071
  • 1
  • 24
  • 37
  • Well, aside from the warning I get when a HiddenField control has no runat="server" attribute, even this is not working for me. At this point, I can't tell if the problem is related to AJAX or what. `Request.Form['EditItemId]` always returns null. – Jonathan Wood Jun 14 '11 at 20:42
  • Sorry about that. You shouldn't use . I'll update the answer now. – marto Jun 14 '11 at 20:47
  • @marto: I must have something else going on as it's still null. I assume it's an AJAX postback I'm getting when it causes the Page's Load event to run. Somehow the data isn't get back to the server. – Jonathan Wood Jun 14 '11 at 20:54
  • Very odd! Have you looked in fiddler whether the value of the hidden field is even sent back to the server? – marto Jun 14 '11 at 20:59
  • I haven't used Fiddler but I can see the Request.Form.AllKeys collection does not contain the hidden field. I even set a default value in the markup but it still does not show. Must be related to being an AJAX callback? – Jonathan Wood Jun 14 '11 at 21:06
  • It must be as it usually works. Try and download fiddler (http://www.fiddler2.com/fiddler2/) It really helps with debugging web applications as you can exactly see what's being sent to the server. – marto Jun 14 '11 at 21:16
1

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

Kieron
  • 24,968
  • 14
  • 72
  • 113
  • I'm assuming Page_Load is being called in response to the AJAX postback. I tried moving the HiddenField controls directly inside the ContentTemplate and even tried again inside EditItemPanel, but I'm getting the same result. – Jonathan Wood Jun 14 '11 at 19:54