javascript ASP.NET 隐藏字段在回发后不更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6380320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:27:28  来源:igfitidea点击:

ASP.NET hidden field not updating after postback

javascript.netasp.netpostback

提问by Jimmy

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

我的 ASP 页面上有一些代码,如下所示:

<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:

我的页面上也有一些 javascript 来执行此操作,以触发更新面板的更新:

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" :

到目前为止一切正常,但在我的 updatepanel1_Load 事件中,我尝试将该值设置回 "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!

回发后,该值永远不会更新并在客户端上设置回 0,我不知道为什么!

Can anyone help? Thanks

任何人都可以帮忙吗?谢谢

回答by Kyle Trauberman

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.

如果您在隐藏字段方面遇到问题,则可以改用 TextBox。使用 css ( display: none;)隐藏文本框以获得与隐藏字段类似的结果。它并不完全漂亮,但它是一个可行的解决方法。

回答by SoftwareGeek

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.

尝试从服务器端调用 registerstartupscript 或类似的东西。我不记得确切的方法名称,但它是页面对象的一部分。这将注册您想在客户端回发后执行的任何 javascript。

回答by Adam Tuliper - MSFT

This similar scenario is done here successfully:

这个类似的场景在这里成功完成:

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

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.

确保您遵循相同的步骤 - 我看不到您的所有代码。首先尝试使用标签以确保它更新为可见控件。如果可行,则使用您的隐藏值缩小范围,以确保隐藏控件的行为没有不同。

回答by user1730026

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

我在代码隐藏中设置了三个 HiddenFields 时遇到问题,但是从 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.

我的问题原来是我的母版页使用了 UpdatePanel,而在我的 ASP.Net Init 事件中,我打算使用条件渲染的 UpdatePanel。

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)

问题是我忘记在设置 HiddenFields 后在我的面板上调用更新。我必须这样做,因为我的按钮是一个部分回发控件(UseSubmitBehaviour=False)

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