Close the ASP.NET and AJAX application browser window.
What is the best way to close the ASP.NET AJAX application browser window after the server side is done.
I found this solution , but it seems a bit tricky for what I want to achieve. Or is this the best way to accomplish my task.
UPDATE: I need to close the window after clicking the button
UPDATE 1: I tried the solution from another SO question and it didn't 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 on my page, but a window pops up "The web page you are viewing is trying to close the window."
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
ScriptManager.RegisterStartupScript(upApproveRequest, typeof(string), "closeWindow", "window.close();", true);
How can this be prevented?
+1
source to share
4 answers
You can actually do this by placing the following code in the 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!";
}
}
+1
source to share
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);
0
source to share