使用 Javascript 部分回发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3965367/
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
Partial postback with Javascript
提问by BeemerGuy
I couldn't find something similar in SO.
我在 SO 中找不到类似的东西。
In ASP.NET, is there any way that on cue I can cause a partial postback with Javascript in an UpdatePanel?
I tried __doPostBack()
but it does a full postback.
I can trick it with a dummy button and fire click()
then handle the partial postback that way, but I want a more graceful way than trickery.
在 ASP.NET 中,有什么方法可以让我在 UpdatePanel 中使用 Javascript 进行部分回发?
我试过了,__doPostBack()
但它做了一个完整的回发。
我可以用一个虚拟按钮来欺骗它并触发click()
然后以这种方式处理部分回发,但我想要一种比欺骗更优雅的方式。
Thanks.
谢谢。
Edit: I found this disturbedbuddha.wordpress.com/2007/11/26/… but I can't get it to work =(
I would love for this method to work; it's perfect for me!
So far what I can do using this last method is gain reference to the timer. With the timer initially disabled, starting the timer doesn't seem to cause a postback. However, without Ajax, if I simply have the timer enabled initially, it posts back at intervals just fine; why can't the Ajax call cause it?
编辑:我发现了这个令人不安的buddha.wordpress.com/2007/11/26/......但我无法让它工作=(
我希望这种方法有效;它对我来说是完美的!到目前为止我可以使用最后一种方法是获得对计时器的引用。在最初禁用计时器的情况下,启动计时器似乎不会导致回发。但是,如果没有 Ajax,如果我只是最初启用计时器,它会每隔一段时间回发一次就好了;为什么 Ajax 调用不能导致它?
回答by Matthew Jacobs
You can use an AsyncPostBackTrigger with the UpdatePanel to do this. Because you need something that can fire an event, using a button is fairly simple and when hidden works nicely.
您可以将 AsyncPostBackTrigger 与 UpdatePanel 一起使用来执行此操作。因为您需要可以触发事件的东西,所以使用按钮相当简单,并且隐藏时效果很好。
If this is your markup:
如果这是您的标记:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<!-- Contents... -->
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ReloadThePanel" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="ReloadThePanel" runat="server" style="display:none;" />
When you want the panel to be updated, you just need to call:
当你想要更新面板时,你只需要调用:
__doPostBack('<%=ReloadThePanel.ClientID %>', null);
This will make ASP.NET think that ReloadThePanel was clicked and the JavaScript auto-generated due to the trigger will handle the rest.
这将使 ASP.NET 认为 ReloadThePanel 被单击,并且由于触发器自动生成的 JavaScript 将处理其余部分。
EDIT
编辑
You can do a pure JavaScript update of the UpdatePanel without any triggers or hidden buttons. You just need to invoke __doPostBack
with the client-side ID as the first argument.
您可以在没有任何触发器或隐藏按钮的情况下对 UpdatePanel 执行纯 JavaScript 更新。您只需要__doPostBack
使用客户端 ID 作为第一个参数进行调用。
__doPostBack('<%=UpdatePanel1.ClientID %>', null);
回答by Oktalist
I used @MatthewJacobs' answer but I found that the call to __doPostBack
caused a runtime exception on IE11. I found that the call to __doPostBack
can be replaced by the following JavaScript call, which worked on both browsers I tested (IE11 and Chrome 52).
我使用了@MatthewJacobs 的回答,但我发现调用__doPostBack
导致 IE11 上的运行时异常。我发现__doPostBack
可以用以下 JavaScript 调用替换对 的调用,该调用适用于我测试的两种浏览器(IE11 和 Chrome 52)。
Sys.WebForms.PageRequestManager.getInstance().beginAsyncPostBack(
[ '<%=UpdatePanel1.ClientID %>' ], '<%=UpdatePanel1.ClientID %>', null
);
See Sys.WebForms.PageRequestManager.beginAsyncPostBack Methodfor more details.
有关更多详细信息,请参阅Sys.WebForms.PageRequestManager.beginAsyncPostBack 方法。