updatepanel 更新后如何调用 javascript 函数 [英] How can I recall a javascript function after updatepanel update

查看:24
本文介绍了updatepanel 更新后如何调用 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:UpdatePanel ID="UpdatePanel2" runat="server"
    OnLoad="UpdatePanel2_Load" UpdateMode="Conditional">
    <ContentTemplate>
        <script type="text/javascript">
            myFunction();
        </script>
        <asp:Label ID="Label1" runat="server" Text="1" Width="18px"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

C#:

if(condition)
{
      UpdatePanel2.Update();
}
protected void UpdatePanel2_Load(object sender, EventArgs e)
{
        Label1.Text += 1;
}

Label1 的值正在改变,但它没有调用 myFunction().我想在某个条件之后再调用一次,但不使用 setTimeout 进行自动调用,有一些想法吗?

the Label1's value is changing but it doesn't call myFunction(). I want to call it one more time after the some condition but not using setTimeout to auto call, some ideas?

推荐答案

当 UpdatePanel 更新 DOM 并将脚本块重写到 DOM 时,它们不会重新执行.您需要获取在 UpdatePanel 完成后触发的客户端事件并重新执行您的 JS 块:

When the UpdatePanel updates the DOM and rewrites script blocks into the DOM, they are not re-executed. You need to get on the client-side event that fires after the UpdatePanel is finished and re-execute your JS blocks:

<script type="text/javascript">
    function handleEndRequest(sender, args)
    {
        var panel = document.getElementById(sender._postBackSettings.panelID);
        var scripts = panel.getElementsByTagName('script');
        for(var i=0;i<scripts.length;i++) {
            eval(scripts[i].innerHTML);
        }
    }

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
</script>

这篇关于updatepanel 更新后如何调用 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆