views:

2657

answers:

4

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 complex for what I want to accomplish. Or is this the best way to accomplish 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)
{
}
A: 

Mmmm ... That's pretty much it. You can just use ScriptManager.RegisterStartupScript(...)

Strelok
A: 

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?

Michael Kniskern
No, this is a browser security item. It might be possible via a round about hack, but I couldn't tell you how.
Mitchel Sellers
I think the page that opened the window has to close it.
Shawn Simon
I am trying to close the parent page. There are no pop-ups in what I am trying to accomplish
Michael Kniskern
A: 

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.

George Stocker
I have to implement a different solution because of this issue. Thank for the input.
Michael Kniskern
No problem. Can I ask why you were trying to close their page?
George Stocker
The user would approve/deny a request in our application and they wanted to automatically close the page when they clicked the approve or deny button.
Michael Kniskern
Why -1? Don't get it.
George Stocker
This is not true.
hyprsleepy
@hyprsleepy Actually, it is. If you do `window.top.close()`, it will tell the user that the window wants to close, but they still have to ok it. That's why my answer is accurate, you still need the user's consent to actually close the window.
George Stocker
@George - No, if you use the code I have in my answer it will close without asking. I have it doing that right now in my website. Try and make a command button with my code in the click event and see what happens when you click on it.
hyprsleepy
+1  A: 

Actually 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!";                
    }
}
hyprsleepy