内联javascript和iframe的Ajax无法成功调用javascript onLoad

问题描述

我有一个包含 iframe 和 javascript 函数用户控件。 js 函数旨在内联在 html 中。 js 函数名称和逻辑特定于该 iframe UC 实例,因此不会与 iframe 或 iframe UC 的其他实例发生重叠。

我已经采用这种方法来处理整页加载和回发。但是,当我通过更新面板(部分页面加载)使用这种方法时,尽管检查后在 html 中可用,但无法识别该功能

这是我的 iframeUC 代码

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucIFrame.ascx.cs" Inherits="CPWeb.ucIFrame" %>
<script type="text/javascript">
    function autoResizeIframe<%=ID%>() {
        var newHeight;
        var newWidth;
        var element = document.getElementById('iframe<%=ID%>');
        if (element) {
            newHeight = element.contentwindow.document.body.scrollHeight;
            newWidth = element.contentwindow.document.body.scrollWidth;
        }

        console.log('autoResizeIframe triggered: ' + 'iframe<%=ID%>' + '; resizing to: ' + newWidth + ' x ' + newHeight);
        element.height = (newHeight) + 35 + "px";
        element.width = (newWidth) + 35 + "px";
    }
</script>
<iframe id="iframe<%=ID%>" src="<%=this.IframeURL%>" width="90%" height="500" class="resizable" frameborder="0" onload="autoResizeIframe<%=ID%>()"></iframe>

我的 iframe 在 btn 点击回发时被添加到 phRoutings 中:

<asp:UpdatePanel ID="upRoutings" runat="server">
                <ContentTemplate>
                    <asp:PlaceHolder ID="phRoutings" runat="server"></asp:PlaceHolder>
                </ContentTemplate>
            </asp:UpdatePanel>

我发现唯一有效的解决方案是调用必须在添加任何 iframe 之前在页面上声明的静态 js 函数,但如果 UC 不包含它的特定要求,这似乎是错误的。

Javascript function not working after postback?

这个问题听起来很相似,但是案例涉及一个预先存在的 iframe,并且建议的解决方案最终是一个静态 js 函数,它只是从脚本管理器而不是从内联调用。无论哪种方式都不是理想的解决方案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)