How to judge whether to close the browser in ASP.NET?

There is no way to capture the background directly, but you can trigger submission when the browser is closed by adding the following script to the page.

& lt script & gt

window.onunload=function(){

if(event . clientx & gt; Customer width & ampervent. client & lt0 || event. altkey)//If you close the browser instead of refreshing the page,

__doPostBack('closing ',' '); //Trigger submission.

}

& lt/script & gt;

Then you can capture this action in the background.

Protected void Page_Load (object sender, EventArgs e)

{

If (request ["__EVENTTARGET"] == "Close")

{

... the last treatment ...

Response. end(); //Because it is the final processing, you don't need page output, so just execute it here.

}

... original normal code ...

}

In some cases, the page will not automatically generate the __doPostBack function or its related controls (for example, when there is only one button control on the page), you can manually add the following code on the page:

& ltinput type = " hidden " name = " _ _ event target " id = " _ _ event target " value = " "/& gt;

& ltinput type = " hidden " name = " _ _ event argument " id = " _ _ event argument " value = " "/& gt;

& ltscript type = " text/JavaScript " & gt;

//& lt; ! [CDATA[

var the form = document . forms[' form 1 '];

If (! The former) {

the form = document . form 1;

}

function _ _ doPostBack(event target,eventArgument) {

If (! the form . on submit | |(the form . on submit()! = false)) {

The former. _ _ event target . value = event target;

The former. _ _ event argument . value = event argument;

the form . submit();

}

}

//]]& gt;

& lt/script & gt;

There is no task method for the user to stop the IE process through the task manager. ...

There is another disadvantage:

1. Triggered only when the user closes the browser. If it is a page jump, it will not be crawled.

2. Of course, you can also change the script to

& lt script & gt

window . onunload = function(){ _ _ doPostBack(' closing ',' '); }

& lt/script & gt;

This will trigger submission no matter what happens, but the problem is bigger, because even if the user submits the page normally, such as clicking a button on the page, this event will be triggered!

3. The solution is to use UpdatePanel, so that ordinary submission will not refresh the page and will not trigger the window.onunload event. But there is another problem, that is, when the page jumps for the first time (whether it is background control or previous user operation), the page will become a blank page. Because that script forced the page to refresh, the jump failed, the background captured the closing parameter, and the output was terminated after processing, resulting in the page becoming a blank page.

4. There is another way to solve the problem of background control jumping. Before that, the front desk users had no way to modify the address bar, so they didn't want to write in detail. I haven't found the perfect solution anyway.

PS: The above method also has a disgusting purpose, which is to put the sentence "response". end(); "After removal, users can leave your page forever (including modifying the address bar and clicking on favorites, etc.). ) unless the browser is closed.

In addition, a solution is provided, which can better realize the processing after page closing or page jumping, but it is relatively troublesome to implement. Is to use ajax as a "heartbeat": when users open your page, save a corresponding timestamp and related information for use when the page is closed. Add a time trigger to the page and submit it with ajax every once in a while (for example, 8 seconds) to refresh the user's corresponding timestamp. (You can also create a timestamp in the global sessionstart and refresh it with sessionid as the identifier, so that it can be triggered not only after the user closes all the windows that open your site, which is very flexible. In addition, a thread is used to check the saved timestamp every once in a while (for example, 20 seconds). If it is found that the difference between each timestamp and the current system time is greater than or equal to the threshold (for example, 20 seconds), it is determined that the user has closed the browser or jumped to another place.

It's a mess I wonder if you can read it?