Michael Kniskern Michael Kniskern - 2 months ago 27
ASP.NET (C#) Question

Programmatically close aspx page from code behind

What is the best way to close an ASPX page from the code-behind?

I have a button event handler that I want to close the page after the user has clicked an ASP.NET button on the page. I have tried to programmatically add a JavaScript method that contains a

window.close()
command to the
OnClientClick
event to close the page but it does not work. The button is also a
asp:AsyncPostBoskTrigger
for an AJAX Update Panel.

The application uses .NET Framework 3.5.

Answer

UPDATE: I have taken all of your input and came up with the following solution:

In code behind:

protected void Page_Load(object sender, EventArgs e)    
{
    Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
}

In aspx page:

function CloseWindow() {
    window.close();
}
Comments