-2

I am really a fresh guy for asp.net. I am using Visual Studio 2012.

I'm creating a login page, were there are two buttons Login and Exit.

When i click the Exit button then application have to be close and also stop the debugging.

My try: referred

I know there are other solutions on the given link for this problem but i prefer the following approach.

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterOnSubmitStatement(typeof(Page), "closePage", "window.onunload = CloseWindow();");
    }

and written the following javascript function within the Login.aspx.

<script language="javascript" type="text/javascript">
    function CloseWindow() {
        window.close();
    }
</script>

Note: The above script works fine if i click on exit button but application also get close when i click Login button and also debugging is not getting stop.

Community
  • 1
  • 1
MAK
  • 6,824
  • 25
  • 74
  • 131

2 Answers2

1

Handle the two buttons separately. Do whatever you want with the login button to submit the page and handle the postback but don't tie any events to the CloseWindow() function. Then, simply create and handle the Exit button like:

<input type="button" onclick="CloseWindow();" value="Exit"/>

The easy answer for your note is to use Internet Explorer as the default launch browser in the debug toolbar.

Unlike a winforms application, an ASP.Net application is stateless. The code that runs in the browser is not dependent on the same resources as the code that is running in the Visual Studio debugger. The only connection between them are the requests that the browser makes to the server (VS 2012 debugger either treats itself as a server or uses IISExpress) and the responses that the server sends back as part of those requests.

In most cases, this means that, when you close the browser, the server keeps on going, waiting for more requests. Internet Explorer works a little differently than the other browsers with Visual Studio. When the IE instance that Visual Studio launches gets closed, the debugger process also closes.

If you really are just starting out the ASP.Net, you should try the ASP.Net MVC framework. It has a cleaner separation between server and client side code, which may help you avoid some of these types of issues.

longestwayround
  • 979
  • 9
  • 13
  • Thank you so much for your detailed answer. Getting an error `Compiler Error Message: CS1026: ) expected`. – MAK Feb 04 '16 at 14:13
  • the example I gave you only had two matching parenthesis, so it looks like you have a missing ")" somewhere else on your page – longestwayround Feb 04 '16 at 21:03
1

Your page is doing exactly what you asked it to do.

In your Page_Load, you called:

ClientScript.RegisterOnSubmitStatement()

That method causes every form submission to execute the script you provided; that means all of your buttons will run your CloseWindow procedure when they are clicked.

If you only want one of your buttons to close the window, then you should only attach the CloseWindow method to one of them. The answer you selected from the linked question only works because there's only one button on the form. I'd recommend you go with one of the other answers, e.g. using:

OnClientClick="javascript:window.close();"

as an attribute on your Exit button.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117