简体   繁体   English

导航离开某个页面时执行c#命令

[英]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." 对于我的项目,我制作了一个表格来删除网站中的帐户,但我希望它将您重新定位到另一个页面,说“帐户x已成功删除”。 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. 因此,我需要保持Sessions仍然为用户名设置,以便能够写入用户的名称而不是“x”,然后当他们离开时,该Session变量将被重置为空。 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? 当您离开某个页面时,有没有办法运行c#命令?

Edit: All this time i've been looking strictly c#, but i now found out there's this thing called unload in javascript. 编辑:这一次我一直在严格看c#,但我现在发现这个东西在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. 您可以简单地从c#代码的会话中获取用户名,将其分配给文本框,然后重置会话变量。 Put the entire code in the page_load event. 将整个代码放在page_load事件中。 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 对于“远离此页面”,如果您有一个公共母版页或基页,则可以检查上一页( 在ASP.NET中获取HTTP Referrer )是否为删除帐户的页面,如果是,则清除会话变量

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. 放弃会话(Session.Abandon()) - 如果这是更好的选择,甚至清除其中的所有设置。

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). 如果您能够回发到访问Session变量的相同页面,那么请确保检查Session是否可用以及您正在查找的变量是否存在(即null检查所有内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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