Link to home
Start Free TrialLog in
Avatar of NAORC
NAORC

asked on

How can I close the current aspx web form?

Hi,

I've an asp.net web app in C#, and I want to have a button which will close the form, thus exiting the application.  Equivalent to the user clicking the X button.

In the button click event I've tried: btn_CloseForm.Attributes.Add("onclick","window.close('FormName.aspx');");

...however, nothing happens the first time this is clicked, and the second time its clicked a messagebox appears asking for confirmation that the user wants to close the form.  I need the form to close immediately on clicking the 'closeForm' button.

Thankyou.
Avatar of Deepak Vasudevan
Deepak Vasudevan
Flag of India image

window.close(); does not take any arguments.

BTW, a browser window opened by you can not be closed, without a browser warning. There are unsupported workarounds like setting a window.opener a value and then closing it.

Try this:

btnCloseForm.Attributes.Add("onClick","self.window.opener = 'a';self.close();");

(But this unsupported workaround may not be supported by future web browsers).
Avatar of NAORC
NAORC

ASKER

Cheers, this doees close the window without a warning, but only once the button has been clicked twice.  The first click causes a page reload, and the second click closes the page.

Is there a way to do this in one click?

Cheers.
ASKER CERTIFIED SOLUTION
Avatar of Deepak Vasudevan
Deepak Vasudevan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial