RegisterStartupScript使window.opener为null

时间:2013-05-21 19:21:02

标签: javascript asp.net vb.net window.opener registerstartupscript

我无法获取window.opener值以在子弹出窗口之间传输。此代码适用于IE 8和订单,但退出IE 9及更高版本。任何输入都表示赞赏。

用不同的方式解释。我妹妹知道我是她的兄弟,但不相信我告诉她我们的父母是谁。

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

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

MainForm.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

1 个答案:

答案 0 :(得分: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

Be(参见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