ASP.NET 页面生命周期问题
显然我不熟悉 ASP.NET 中页面的生命周期。当我想在离开页面后处理会话变量时,这一点变得很明显。我做了最有意义的事情:
protected void Page_Unload(object sender, EventArgs e)
{
Session.Remove("ServiceSearch");
}
我不知道的是当我从 AND 转到页面时会调用它。我想要做的是每当用户离开页面时处理该 Session 变量。我该怎么做?
Apparently I am not familiar with the Life Cycle of a page in ASP.NET. This became apparent when I wanted to dispose of a Session variable after I left the page. I did what made the most sense:
protected void Page_Unload(object sender, EventArgs e)
{
Session.Remove("ServiceSearch");
}
What I didn't know is that this would be called when I go from AND to the page. What I am wanting to do is dispose of that Session variable whenever the user leaves the page. How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Page_Unload
是指在解析和创建页面后将Page
对象释放之前将其卸载。它与离开页面无关。就像@Nick 所说,除了控制每个出口路径之外,确实没有什么好的方法可以说明这一点。你不能,因为你无法控制用户何时回击,或者何时访问 google.com,然后将他们刚刚所在的网址粘贴到浏览器中,等等。如果你想删除
Session
变量,这样它就不会被无意中重新使用,更好的解决方案是每次进入页面时覆盖Session
变量,并让它在会话开启时被释放会话到期时它自己的时间。Page_Unload
refers to unloading thePage
object right before it is disposed after parsing and creating the page. It has nothing to do with leaving the page. Like @Nick says, there is really no good way to tell that, except to control every exit path. And you can't, because you can't control when the user hits back, or goes to google.com and then pastes in the url they were just at into the browser, etc.If you want to remove the
Session
variable just so it doesn't get re-used unintentionally, a better solution is to overwrite theSession
variable every time you enter the page, and just let it be disposed with the session on its own time when the session expires.会话数据对于存储超出页面生命周期的数据非常有用。如果您不想在页面生命周期之外存储它,那么会话数据不适合您。
Session data is useful for storing data beyond the lifetime of a page. If you don't want to store it beyond the life of a page then Session data is not for you here.
如果用户通过链接离开页面,您可能会创建一个链接按钮,该按钮连接到同一页面上的方法。该方法将删除会话并进行重定向。
我希望有更好的解决方案。不过,据我了解,在您的情况下没有可使用的页面事件,因为页面必须重新加载才能执行删除会话代码。当用户通过某个链接离开时,页面不会重新加载。
您或许可以通过 javascript 来处理它。我曾经遇到过想要离开的情况,但我收到了一个关于一些废话的弹出框。您可能可以使用相同的技术来触发 AJAX 来删除会话。
If the user leaves the page via a link you could possibly create a link button that is hooked up to a method on the same page. That method would remove the session and do the redirect.
I would hope there is a better solution though. Although, from my understand, there is no page event to use in your case because the page would have to reload to execute the remove session code. When the user leaves via some link the page is not reloaded.
You may possibly be able to handle it via javascript. I've been in situations where I wanted to leave and I got a popup box about some bs. You could probably use the same technique to fire AJAX to remove the session.
我在 php 登录时遇到了类似的情况 - 登录用户可以使用来回箭头来登录页面。因此,我只需在登录页面的 html 脚本上方添加 :
,这样,如果登录用户用箭头返回,会话就会被取消设置并被销毁。任何向前箭头的尝试都只会导致登录页面 - 请求用户登录凭据 - 本质上,用户每次向后箭头都会注销!
希望这有帮助!
阿尔弗雷德
I faced a similar situation on php login- a logged-in user could use the arrow back and forth to login page. So, I simply add :
above the html script of the login page so that if the logged-in user arrowed back, the session is both unset and destroyed. Any attempt to arrow forward will only lead to the login page-requesting user login credentials-essentially, the user is logged out any time they arrow back!
Hope this helps!
Alfred