Link to home
Start Free TrialLog in
Avatar of shaik basha
shaik basha

asked on

Actually i have a requirement like for example I have a login page and i have logged in, now while clicking logout button i have to do some database updation and suddenly without clicking the logout

Actually i have a requirement like for example I have a login page and i have logged in,

now while clicking logout button  i have to do some database updation and suddenly without clicking the logout button if the user

closes the window no updation will take place.

How to detect the browser close event in ASP.Net (C#) . aspx.cs file
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

If I understood clearly, UPDATE should happen only if the user clicks Logout button and not for Browser close events..
So simply, have your UPDATE logics called out only for the Logout button click event..
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>How to detect the browser close event in ASP.Net (C#) . aspx.cs file

You can't but that is OK since you said you don't want the update to take place.  So there is no code to write.
Avatar of shaik basha

ASKER

UPDATE should happen  if the user clicks Logout button and also in browser close event in asp.net
i want to update happend in the browser close event and also in logout button click . if we click in the browser close click i want to rise some method in aspx.cs file then i will make update the server side guide me.
>> UPDATE should happen  if the user clicks Logout button and also in browser close event in asp.net

If you want to do the UPDATE for browser close event as well, then try this way as you can't capture browser close event while closing the browser.
1. In your IIS or Application Server, maintain a list of active sessions by recording their host name or ip.
2. For any browser close event, active session list will get reduced by 1 and ensure that the UPDATE is being carried out with the values stored in cache..
Yes, you can try this approach..
Please be noted that while doing so, you might need to capture the values stored in that page as well to perform your UPDATE operation..
Again, you cannot trap clicking the 'X'.

>>sir if i follow below link it is possible or not

That uses the onunload JavaScript event.  That will perform your update for more than just closing the browser or clicking the logout button.

https://www.w3schools.com/jsref/event_onunload.asp
The onunload event occurs once a page has unloaded (or the browser window has been closed).


onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.).
To start with create a new asp.net page and add an instance of the ScriptManager to it. Now configure the ScriptManager to enable calling of page methods marked with WebMethod attribute.


Hide   Copy Code
<asp:scriptmanager id="Scriptmanager1" runat="server" enablepagemethods="true" xmlns:asp="#unknown"></asp:scriptmanager>


The next step is to create a web method on the server. The method needs to be marked with the WebMethod attributte.

Hide   Copy Code
[WebMethod]
public static void BrowserCloseMethod()
{
    // Write Custom Code
    HttpContext.Current.Session.Abandon();
}  


Finally call the server method on closing the client browser window. Add a javascript method and call this method when the unload event for body of the browser is called.

Hide   Copy Code
<script language="javascript" type="text/javascript">
function Browser_Close()
{
      PageMethods.BrowserCloseMethod();
}
</script>

<body onunload="Browser_Close()"></body>

i am making the user if he is clicking on the master page open new window in the new window page load event user updation done to "active "  then again browser or window close at the i want to update in database to "inactive"  how achive this give me example  already make user active in page load  i want to make user inactive in database when browser close event  at the time of beowser close event any method rised in aspx.cs then it is possible please give solution for this
I'll keep repeating it:  You CANNOT trap a browser close.  Stop trying.  It cannot be done.

If you absolutely need the inactive flag set as some requirement, I would look at changing the requirement.

Possibly an inactive page timer that will auto-logout after X minutes of inactivity.  Then a "last_active" flag in the database.  So, you will know that any last_active flag older than your page auto-logout is automatically "inactive"?  Granted, that can be beaten by disabling JavaScript but so can the codeproject example.
please give me example for my requirement
I cannot give you an example for something that doesn't exist.  As you have described what you want, there is no solution.

You need to think about changing the requirement.
sir your saying follow something like this

Possibly an inactive page timer that will auto-logout after X minutes of inactivity.  Then a "last_active" flag in the database.  So, you will know that any last_active flag older than your page auto-logout is automatically "inactive"?  Granted, that can be beaten by disabling JavaScript but so can the codeproject example.

please example for that i will follow thank you in advance
I don't have the time to write a sample test case for you.

I just posted that off the top of my head and having thought about it, it will not be a 100% accurate approach and will require a LOT more monitoring.

Create a last_active date/time column in the database.  Set a database trigger to update that column on any database access.

If you create a JavaScript timer that will log the user out after 5 minutes of inactivity and the user closes the browser after 4 minutes of activity, then any last_activity older than 5 minutes is likely a closed browser?


There might be better methods of tracking user inactivity out there if you Google around.

I guess you need to ask is if this requirement is actually necessary?

For a minute, say to can capture the browser close (which you cannot), how do you handle the situation when I unplug my computer?  There is no click involved.  The Browser/OS/??? just goes away.  Then they would show "active" forever because there wasn't any "event" that caused it to end.

I would rethink what you are wanting to get with the "inactive" flag and see if there are better ways to get that metric.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.