简体   繁体   English

从iFrame的UpdatePanel中调用父页面javascript

[英]Call parent page javascript from within UpdatePanel inside iFrame

I have come across a issue calling javascript from within my UpdatePanel, that is within an iFrame. 我遇到了在iFrame中从我的UpdatePanel调用javascript的问题。 I can call the parent page javascript without issue from within the iFrame using parent.functionName(). 我可以使用parent.functionName()从iFrame调用父页面javascript,而不会出现问题。

This is inside my pageName.aspx.cs. 这是在我的pageName.aspx.cs中。 (Code by KP ( ASP.Net - Javascript inside AJAX UpdatePanel )) (由KP提供的代码( ASP.Net-AJAX UpdatePanel中的Javascript ))

protected override void Page_Load(object sender, EventArgs e)
{
    base.Page_Load(sender, e);

    StringBuilder scriptStringB = new StringBuilder();
    scriptStringB.Append("<script type=\"text/javascript\">");
    scriptStringB.Append("function CloseiFrame() {");
    scriptStringB.Append(" alert(\"CloseiFrame\");");
    scriptStringB.Append(" CloseiFrameThroughParent();");
    scriptStringB.Append("}");
    scriptStringB.Append("</script>");

    RegisterClientStartupScript("CloseiFrame", scriptStringB.ToString());
}
private void RegisterClientStartupScript(string scriptKey, string scriptText)
{
    ScriptManager sManager = ScriptManager.GetCurrent(this.Page);

    if (sManager != null && sManager.IsInAsyncPostBack)
    {
        //if a MS AJAX request, use the Scriptmanager class
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), scriptKey, scriptText, false);
    }
    else
    {
        //if a standard postback, use the standard ClientScript method
        scriptText = string.Concat("Sys.Application.add_load(function(){", scriptText, "});");
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), scriptKey, scriptText, false);
    }
}

I have tried other ways, using the Microsoft way ( http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx ) for example. 我尝试了其他方法,例如使用Microsoft方法( http://msdn.microsoft.com/zh-cn/library/z9h4dk8y.aspx )。

In pageName.aspx it looks like this: 在pageName.aspx中,它看起来像这样:

<form id="Form1" class="iFrameForm" method="post" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            ...Scripts are included here
        </Scripts>
    </asp:ScriptManager>
    <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
        <ContentTemplate>
            <asp:imagebutton id="ExitButton" runat="server" 
                           ImageUrl="~/exit.png" 
                           AlternateText="<%$ Resources:Resource, Exit %>" ToolTip="<%$ Resources:Resource, Exit %>" OnClick="Exit_Click" >
            </asp:imagebutton>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

Of course there is more in there, but these are the basics. 当然还有更多,但这是基础。 The button is generated and at the end of the form 'action' it has "jscriptfunction=CloseiFrame". 该按钮已生成,并且在“操作”形式的末尾具有“ jscriptfunction = CloseiFrame”。

But there is no alert, let alone the function being called. 但是没有警报,更不用说调用函数了。 I could of course use a service to work around all of this, but I would really like to solve this issue within the pages&javascript. 我当然可以使用一项服务来解决所有这些问题,但是我真的很想在pages&javascript中解决此问题。

'A' solution has been found. 找到了“ A”解决方案。 Using the Page_Load to check if it is a postback or a normal pageload I can call on a javascript function from there. 使用Page_Load检查它是回发还是正常的页面加载,我可以从那里调用javascript函数。

protected override void Page_Load(object sender, System.EventArgs e)
{
    base.Page_Load(sender, e);

    if (IsPostBack) //If it is a postback close the popup
    {
        StringBuilder scriptStringB = new StringBuilder();
        scriptStringB.Append("<script type=\"text/javascript\">");
        scriptStringB.Append(" CloseiFrameThroughParent();");
        scriptStringB.Append("</script>");

        RegisterClientStartupScript("CloseiFrameThroughParent", scriptStringB.ToString());
    }
}

But I must say, it isn't pretty... 但是我必须说,这不漂亮...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM