【问题标题】:Why doesn't this function get called on async postbacks为什么在异步回发时不调用此函数
【发布时间】:2017-04-11 15:34:46
【问题描述】:

我正在尝试为在我的页面上执行回发的内容创建加载屏幕,特别是通过 gridview 的行单击来执行回发并导致整个页面加载。我想要某种加载页面或在页面上加载 sn-ps。从谷歌搜索/stackoverflowing 中,我从其他帖子和资源中汇总了这些内容。

脚本

<script type="text/javascript">

    $(function () {

        // Get the instance of PageRequestManager.
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        // Add initializeRequest and endRequest
        prm.add_initializeRequest(prm_InitializeRequest);
        prm.add_endRequest(prm_EndRequest);

        console.log(prm);
        // Called when async postback begins
        function prm_InitializeRequest(sender, args) {
            console.log("init sender " + sender);
        }

        // Called when async postback ends
        function prm_EndRequest(sender, args) {
            console.log("end sender: " + sender);    
        }

    });

</script>

gridview/更新面板

<asp:UpdatePanel ID="actAddPanel" runat="server" 
updatemode="Conditional" 
ChildrenAsTriggers="true">
  <ContentTemplate>
     <asp:GridView></asp:GridView>
   </ContentTemplate>
</asp:UpdatePanel>

问题

脚本应该让我知道回发何时开始和结束,所以理论上prm_InitalizeRequest 将允许我在回发发生时修改页面上的元素以创建自定义加载动画/细节,prm_EndRequest 会让我知道当回发结束时,我知道何时结束该自定义动画/细节。

我使用ChildrenAsTriggers="true" 制作了gridview 触发器的行,但是当我进行回发时,我看不到我的console.log 消息,这些消息让我知道这些函数实际上已被调用。如何让这些功能发挥作用?

【问题讨论】:

  • 您可以尝试将 javascript 代码移出 $(function (){}) 吗?据我所知,$(function (){}) 仅在初始加载时触发,更新面板的后续部分更新不会触发 $(function (){})
  • @Chih-HoAndyChou 当我这样做时,Sys 未定义
  • 尝试在 ScriptManager 控制后移动你的脚本块。我刚刚尝试过,它按预期工作。
  • @Chih-HoAndyChou 我刚刚尝试过,但仍然无法按预期工作
  • 你能分享你的代码吗?

标签: javascript c# jquery asp.net gridview


【解决方案1】:

我发现为了执行这些功能,它们需要由事件触发。在我的 gridview 中,我添加了Triggers,然后我的功能按预期工作。

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 2016-08-12
    相关资源
    最近更新 更多