如何在关闭弹出窗口后执行一组命令



我在页面中有一个按钮,当我点击它时,它会执行一组命令,然后打开一个弹出页面。

protected void Button2_Click(object sender, EventArgs e)
{
/*set of commands*/
string arry = String.Join(",", ((string[])a1.ToArray(typeof(String))));
string url = "AreaGridValue.aspx?list="+ arry;
string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
}

现在我想在关闭弹出窗口后执行更多的代码集。我该如何做到这一点而不丢失父页面中的值。

您可以将打开的弹出窗口命名为:

var popup = window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');

并使用setInterval()检查窗口是否关闭并运行其他代码:

var int = setInterval(function(){ if(popup.closed === true) { clearInterval(int); /*write your code here*/} } ,50);

我省略了将这些代码放在c#字符串中的部分,并将javascript部分作为c#部分中的新内容编写。

所以最终会是这样的:

string url = "AreaGridValue.aspx?list="+ arry;
string s = "var popup = window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
s += "var int = setInterval(function(){ if(popup.closed === true) { clearInterval(int); alert('popup closed');} } ,50);";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);

因此,当弹出窗口关闭时,setInterval中的功能将被执行,在上面的例子中,将显示一个警告,说"弹出窗口关闭"。

最新更新