位置.重新加载事件触发按钮单击事件

location.reload event triggers the button click events

本文关键字:事件 按钮 单击 加载 新加载 位置      更新时间:2023-09-26

我有一个简单的要求。关闭后需要从子窗口刷新父页面。我正在从这样的按钮单击事件打开一个子窗口

ClientScript.RegisterStartupScript(this.GetType(), "showPage", "javascript:showPage();", true);

父页面中的 JavaScript 方法是

function showPage() {
        window.open("Page2.aspx", "Shan", "width=200,height=200,scrollbars=no");
    }

在子页面中,我有

function refreshAndClose() {
        window.opener.location.reload();
        self.close();
        //window.opener.location.href = "Default.aspx"
        //self.close();
    }

并调用此

<body onunload="refreshAndClose();">

现在它按预期工作,当我关闭子窗口时,它会调用父窗口的页面加载事件,但它也会进入按钮单击事件(我调用子窗口的按钮单击事件)并再次执行 javascript 方法。因此,子窗口再次出现,并且递归进行。

如何避免这种情况?我 asp.net。请帮助我。

提前谢谢。

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "showPage", "javascript:showPage();", true);
            }
        }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication4.WebForm3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        var myWindow;
        function showPage() {
            myWindow = window.open("Page2.aspx", "Shan", "width=200,height=200,scrollbars=no");
            setInterval(function () {
                alert("ap");
                if (!myWindow) {
                    document.getElementById("msg").innerHTML = "'myWindow' has never been opened!";
                } else {
                    if (myWindow.closed) {
                        document.getElementById("msg").innerHTML = "'myWindow' has been closed!";
                        form1.submit();
                    } else {
                        document.getElementById("msg").innerHTML = "'myWindow' has not been closed!";
                    }
                }
            }, 5000);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div id="msg"></div>
        </div>
    </form>
</body>
</html>