Ishika
Ishika

Reputation: 145

close current web form in asp.net

How i can close current web form in web based application? I have tried with following code:

mybutton.Attributes.Add("onclick","window.close()")

But its not working Help me. Thank you.

Upvotes: 2

Views: 16689

Answers (2)

János Nagy
János Nagy

Reputation: 146

You can use the following script to close the current window in Chrome and IE (I checked in IE8).

mybutton.Attributes.Add("onclick", "window.open('', '_self');window.close();");

but it still won't work in Firefox. See more info on this: How can I close a window with Javascript on Mozilla Firefox 3?

But because it is a workaround (as Kangkan said window.close() should be used only to close popup window created by window.open() ), it is not a reliable solution and can be broken if a new browser version is out.

Bottom line: there is no reliable, universal solution for your problem, there is no proper way to close a non-popup window from javascript. If you can rely on the fact that your site will be used only in IE, Chrome, etc. you can use a workaround/hack (see above - but don't forget, it can easily be broken in next browser release), otherwise you should consider a different approach of the problem, instead of closing the window.

Upvotes: 0

Kangkan
Kangkan

Reputation: 15571

You can use window.close() to close a pop-up window only. If you really need to close the window, use a pop-up instead. However review why the closing is important? You may consider a redirection (either server.transfer or response.redirect).

Update:

Looking at the discussion at this stage, we need to see the relevance of why wee need to close the window as desired. What is the business value that we are achieving?

Upvotes: 2

Related Questions