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


No, there is no way to close the browser window without the user's consent. You can take them out of your application, but you cannot force the browser window to close.



+1


source


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


That's pretty much it. You can just useScriptManager.RegisterStartupScript(...)

-2


source







All Articles