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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.