0

我无法让 window.opener 值在子弹出窗口之间传输。此代码在 IE 8 和 order 中工作,但在 IE 9 和更高版本中停止工作。任何输入表示赞赏。

用另一种方式解释。我姐姐知道我是她的兄弟,但不相信我会告诉她我们的父母是谁。

处理流程:
父窗口(主窗体)-> 打开弹出窗口(PopUp 1)
用户单击 PopUp 1 上的按钮以打开 PopUp 2。
PopUp 2 打开:
    它从 PopUp 1 中提取数据然后关闭 PopUp 1
用户单击按钮(保存) PopUp
2。PopUp 2 刷新原始父“主窗体”上的数据。然后自行关闭。

最后一步是不影响 IE 9 的步骤。我创建了下面的示例以显示该功能使用纯 JavaScript 工作,但在回发并使用 RegisterStartupScript 调用 JavaScript 函数时失败。我们使用 RegisterStartupScript 是因为表单的保存操作会根据用户的输入产生许多不同的结果。
所有页面都在同一个域中。

主窗体.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="MainForm.aspx.vb" Inherits="MainForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
    <script type="text/javascript">
        function OpenPopUp() {
            document.getElementById('lblStatus').innerHTML = '';
            window.open('PopUp1.aspx', '',
                 'width=750,left=' + ((screen.width / 2) - 375) + ',top=' + ((screen.height / 2) - 250) +
                 ',height=500,location=no,menubar=no,status=yes,scrollbars=yes,toolbar=no,resizable=yes');
        }

        function Refresh() {
            document.getElementById('lblStatus').innerHTML = 'It Worked';
        }

</script>
</head>
<body>
<form id="form1" runat="server">
    <input type="button" id="btnOpen" value="Open Popup" style="width: 110px" onclick="OpenPopUp()" />
    <br /><br />
    <asp:Label ID="lblStatus" runat="server"></asp:Label>
</form>
</body>
</html>

PopUp1.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="PopUp1.aspx.vb" Inherits="PopUp1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
        <script type="text/javascript">
            function OpenPopUp2() {
                window.open('PopUp2.aspx', '', 'width=700,left=' + ((screen.width / 2) - 350) + ',top=' +
                ((screen.height / 2) - 275) + ',height=550,location=no,status=yes,toolbar=no,resizable=no,scrollbars=yes');
            }
    </script>
</head>
<body>
    <h1>This is pop up 1</h1>
    <form id="form1" runat="server">
        <asp:TextBox ID="txtPop1" runat="server" Text="test 1" ></asp:TextBox>
        <input type="button" id="btnOpen" value="Open Popup 2" style="width: 110px" onclick="OpenPopUp2()" />
    </form>
</body>
</html>


PopUp2.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="PopUp2.aspx.vb" Inherits="PopUp2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
    function GrabData() {      
        var oParentDoc = window.opener.document;
        if (oParentDoc != null) {
            try { document.getElementById('txtPop2').value = oParentDoc.getElementById('txtPop1').value; } catch (ex1) { }
        }     
        var oParentOpener = window.opener.opener;
        window.opener.close();
        window.opener = oParentOpener;
    }

    function MyClose() {
        window.opener.Refresh();
        window.close();
    }
</script>
</head>
<body>
<h1>This is pop up 2</h1>
<form id="form1" runat="server">
    <asp:TextBox ID="txtPop2" runat="server" ></asp:TextBox>
    <input type="button" id="btnSave1" value="Save Working" style="width: 110px" onclick="MyClose()" />       
    <asp:Button ID="btnSave" runat="server" Text="Save Not Working" />
</form>
</body>
</html>


PopUp2.aspx.vb

Partial Class PopUp2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack() Then
            ClientScript.RegisterStartupScript(Type.GetType("System.String"), "Refresh", _
                    "<script type=""text/javascript"">GrabData();</script>")
        End If
    End Sub
    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "RestoreFilter", _
            "<script type=""text/javascript"">MyClose();</script>", False)
    End Sub
End Class
4

1 回答 1

0

这不应该:

Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
    ScriptManager.RegisterStartupScript(Me, Me.form1.GetType(), "RestoreFilter", _
        "<script type=""text/javascript"">MyClose();</script>", False)
End Sub

是(参见 GetType 部分):

Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "RestoreFilter", _
        "<script type=""text/javascript"">MyClose();</script>", False)
End Sub
于 2013-05-21T19:29:23.243 回答