How can I close the current aspx web form?


I've an 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.

Who is Participating?
With a sample HTML like this,

   <input type="button" value="Close" onClick="self.window.opener='a';self.close();">

it does respond in a single click.

Did you check the following:

(*) Any other event or event bubbling in effect?
(*) onDblClick being handled?
(*) MouseClick speed  (in Control Panel)
(*) An equivalent in <a onClick="alert('test')">test</a> to ensure that how many times the alert appears.
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).
NAORCAuthor Commented:
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?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.