admin管理员组文章数量:1425996
What is the best way to close a browser window of an AJAX ASP.NET application after the server-side has been executed.
I found this solution, but it seems a little plex for what I want to acplish. Or is this the best way to acplish my task.
UPDATE: I have to close the window after the button is pressed
UPDATE 1: I tried the solution from the other SO question, and it did not work for me.
<asp:Button ID="btnMyButton" runat="server" onClick="btnMyButton_Click" />
protected void btnMyButton_Click(object sender, EventArgs e)
{
}
I used the following code in my page, but the "The webpage you are viewing is trying to close the windows" module window pops up.
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
ScriptManager.RegisterStartupScript(upApproveRequest, typeof(string), "closeWindow", "window.close();", true);
Any way to prevent this?
What is the best way to close a browser window of an AJAX ASP.NET application after the server-side has been executed.
I found this solution, but it seems a little plex for what I want to acplish. Or is this the best way to acplish my task.
UPDATE: I have to close the window after the button is pressed
UPDATE 1: I tried the solution from the other SO question, and it did not work for me.
<asp:Button ID="btnMyButton" runat="server" onClick="btnMyButton_Click" />
protected void btnMyButton_Click(object sender, EventArgs e)
{
}
I used the following code in my page, but the "The webpage you are viewing is trying to close the windows" module window pops up.
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
ScriptManager.RegisterStartupScript(upApproveRequest, typeof(string), "closeWindow", "window.close();", true);
Any way to prevent this?
Share Improve this question edited May 23, 2017 at 10:33 CommunityBot 11 silver badge asked Dec 9, 2008 at 23:49 Michael KniskernMichael Kniskern 25.3k70 gold badges169 silver badges233 bronze badges 1- @Gortok - I came up a solution to sets the OnClientClick event with the JavaScript function of the button that closes the child window when it is opened from the parent page. – Michael Kniskern Commented Jul 23, 2009 at 21:02
4 Answers
Reset to default 2Actually you can do this by placing the following code in your button click event.
protected void btnMyButton_Click(object sender, ImageClickEventArgs e)
{
// Update database
bool success = Presenter.DoDatabaseStuff();
if (success)
{
// Close window after success
const string javaScript = "<script language=javascript>window.top.close();</script>";
if (!ClientScript.IsStartupScriptRegistered("CloseMyWindow"))
{
ClientScript.RegisterStartupScript(GetType(),"CloseMyWindow", javaScript);
}
}
else
{
// Display failure result
result_msg_area.Visible = true;
lblError.Text = "An error occurred!";
}
}
No, there is no way to close a browser window without the user's consent. You can log them out of their application, but you can't forcibly close the browser window.
To avoid the script warning, you can use this:
window.open('', '_self', '');window.close();
So:
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
ScriptManager.RegisterStartupScript(upApproveRequest, typeof(string), "closeWindow", "window.open('', '_self', '');window.close();", true);
That's pretty much it. You can just use ScriptManager.RegisterStartupScript(...)
本文标签: javascriptClose a browser window of an ASPNET and AJAX applicationStack Overflow
版权声明:本文标题:javascript - Close a browser window of an ASP.NET and AJAX application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745365845a2655510.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论