How can I close the current aspx web form?

Posted on 2006-04-26
Last Modified: 2008-02-01

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.

Question by:NAORC
    LVL 15

    Expert Comment

    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).
    LVL 5

    Author Comment

    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?

    LVL 15

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now