简体   繁体   中英

Executing c# commands when navigating away from a certain page

I've checked around the entire web and could not find an answer for this.

For my project, i made a form to delete an account in a website, but i want it to relocate you to another page saying "the account x had been deleted successfully." Thus, i need to keep the Sessions still set for the user name, to be able to write the name of the user instead of that "x", and then when they navigate away, that Session variable will be reset to nothing. But how? Of course, i can set on every button possible to delete it, but that's just stupid. Is there a way to run c# commands when you navigate away from a certain page?

Edit: All this time i've been looking strictly c#, but i now found out there's this thing called unload in javascript. Question is: how can i reset the session variable using that?

You may simply get the username from session on the c# code, assign it to a textbox and then reset the session variable. Put the entire code in the page_load event. For the "get away from this page", if you have a common masterpage or base page, you may check if the previous page ( Getting the HTTP Referrer in ASP.NET ) is the page for delete account and if yes clean the session variable

If you have a 'Delete My Account' button on a page, simply post back on the button click.

In the button click's event handler assign what values you wish to local variables from the current Session - Name, Date of Membership, Expiry Date etc.

Abandon the Session (Session.Abandon()) - or even clear all settings in it if that is a better option.

Use the variables to populate any labels, spans etc. on your returning web page. This could be the same page - or a redirect to a different page. If redirecting, pass the values you need to the next page to use in your output.

If you will be able to post back to the same pages that were accessing the Session variables, then ensure that you check that the Session is available and that the variables you are looking up exist (ie null check everything).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM