0

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?

Yuval Roth
  • 75
  • 1
  • 9

2 Answers2

0

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

Community
  • 1
  • 1
bdn02
  • 1,500
  • 9
  • 15
  • Assigning it to a textbox sounds like a hideous security vulnerability... everyone could just edit the html code and probably do something with it.. even though i don't understand how is that not different than setting another variable. – Yuval Roth Mar 07 '15 at 19:12
  • You may assign it to a label o something else. The only purpose of this is get the username from session and show it to the page and immediately next clear the session variable [in the same function] – bdn02 Mar 07 '15 at 19:19
0

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 (i.e. null check everything).

Steve Padmore
  • 1,710
  • 1
  • 12
  • 18