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
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
>>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.
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.
ASKER
UPDATE should happen if the user clicks Logout button and also in browser close event in asp.net
ASKER
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..
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..
ASKER
sir if i follow below link it is possible or not
https://www.codeproject.com/Tips/89661/Execute-server-side-code-on-close-of-a-browser
https://www.codeproject.com/Tips/89661/Execute-server-side-code-on-close-of-a-browser
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..
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
>>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.).
ASKER
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 :scriptman ager>
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.Sessio n.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.BrowserCloseMe thod();
}
</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
Hide Copy Code
<asp:scriptmanager id="Scriptmanager1" runat="server" enablepagemethods="true" xmlns:asp="#unknown"></asp
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.Sessio
}
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.BrowserCloseMe
}
</script>
<body onunload="Browser_Close()"
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.
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.
ASKER
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.
You need to think about changing the requirement.
ASKER
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
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.
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 TRIALMembers 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.
So simply, have your UPDATE logics called out only for the Logout button click event..