ASP.NET hidden field not updating after postback

断了今生、忘了曾经 提交于 2020-01-02 06:11:28

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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()


来源:https://stackoverflow.com/questions/6380320/asp-net-hidden-field-not-updating-after-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!