4
votes

I have some code on my ASP page which looks like this:

<asp:UpdatePanel runat="server" id="updatepanel1" UpdateMode="Conditional" onload="updatepanel1_Load" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:HiddenField id="sendingRequest" runat="server" Value="0" />
....
</ContentTemplate>
</asp:UpdatePanel>

I also have some javascript on my page which does this, to trigger the update of the updatepanel:

var sendingRequest = document.getElementById("<%=sendingRequest.ClientID%>");
sendingRequest.value = "1";
__doPostBack('<%= updatepanel1.ClientID %>', '');

Everything works fine up to now, but in my updatepanel1_Load event, I try to set the value back to "0" :

sendingRequest.Value = "0";

This value never gets updated and set back to 0 on the client after the postback, and I can't figure out why!

Can anyone help? Thanks

4
it's not a duplicate.. that was a problem with getting a hidden field's value when dynamically creating it in the server side code. Here my problem is that the hidden field's value isn't updating when created client side in the UpdatePanel ContentTemplate container - Jimmy
are you sure is not being set back to 1 (or loaded from viewstate) after you set it back to 0 on the server? - Jaime
not intentionally.. could this problem be to do with there being a second updatepanel on the same page which is being refreshed every 5 seconds? i don't see why it should affect the updating of the hiddenfield though since the hiddenfield is in a separate updatepanel - Jimmy
Is there any way you could post the complete .aspx page and code behind? If nothing else, it will give us a little better context when reviewing your code. Perhaps we could spot a better solution for you. - Kyle Trauberman

4 Answers

5
votes

If you're having problems with a hidden field, you could use a TextBox instead. Hide the textbox with css (display: none;) to achieve similar results to a hidden field. Its not exactly pretty, but its a workable workaround.

0
votes

Try to call registerstartupscript or something like that from server side. I can't remember exactly the method name but its part of page object. This will register any javascript you would like to execute after postback on the client side.

0
votes

This similar scenario is done here successfully:

http://encosia.com/easily-refresh-an-updatepanel-using-javascript/

Ensure you are following the same steps - I can't see all of your code. Try with a label first to make sure it gets updated as a visible control. If that works then narrow it down with your hidden value to make sure the behavior isn't different for a hidden control.

0
votes

I had an issue with three HiddenFields being set in Code-Behind, but their values were not set when polled from JQuery.

My issue turned out being that my Master Page uses an UpdatePanel, and in my ASP.Net Init event I was purposing that UpdatePanel with conditional rendering.

Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    mstr = CType(Master, Site)

    'setup partial rendering so Log can update asynchronously
    scriptManager = CType(mstr.FindControl("ScriptManager1"), ScriptManager)
    scriptManager.EnablePartialRendering = True
    scriptManager.AsyncPostBackTimeout = 28800
    CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).UpdateMode = UpdatePanelUpdateMode.Conditional
    CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).ChildrenAsTriggers = False
End Sub

The issue was that I forgot to then call update on my panel after setting the HiddenFields. I had to do this because my button was a partial-postback control (UseSubmitBehaviour=False)

    hfParams.Value = paramlist.ToString()
    hfForms.Value = formlist.ToString()
    hfStartJob.Value = "True"
    CType(mstr.FindControl("UpdatePanel1"), UpdatePanel).Update()