Close window on Button click in ASP DOT NET.

Posted by Chikul under ASP.NET category on | Views : 10046
To close a browser window there is only one Way i.e to add javascript.

Below are the two methods of making a browser window (page) close.

Method 1 : On page load add the script to the button onclick attribute . It will occur a bit faster.

Method 2 : If User wants to validate some Process before Window close, Its better to use Method 2 ,Because using the first Method no serverside code will get executed.
Here On Button Click Use the Script to close the window.
So Before closing the window we can Validate the Process in the Applications

Codes :

Partial Class Default5
Inherits System.Web.UI.Page

'Method 1 : On Page_Load add the Attribute
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnClose.Attributes.Add("onclick", "window.close();")
End Sub

'Method 2 : on Button Click Use the Script
Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
If Page.IsValid Then
Response.Write("<script language='javascript'> { window.close();}</script>")
End If
End Sub
End Class

Comments or Responses

Login to post response