ASP.NET - Code Samples - Window Close

ASP.NET Close window on button click.

Synopsis:

The only way to close a browser window is to add javascript. The script can be added to the button onclick attribute on page load but this presents the problem that no serverside code will get executed but it will occur a bit faster. Below are the two methods of making a browser window (page) close.

The Code:

   Private Sub cancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
		Handles cancelButton.Click

        Response.Write("<script language='javascript'> { window.close();}</script>")
   End Sub

In Form_Load
	cancelButton.Attributes.Add("onclick", "window.close();")
About this page: