会话对象不更新 ASP.NET
我在代码中的一个时刻设置了一个会话对象:
Session("my_name") = "Dave"
稍后在我的代码中,我给用户一个更新该对象的机会:
Session("my_name") = TextBox1.Text
我重新加载页面并显示一个像这样的小语句:
Label1.Text = "Hello" & CStr(Session("my_name"))
结果是:“Hello Dave”无论如何我也改变了 Session("my_name") 。
编辑:这是我编写的演示的完整代码隐藏:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1)
If Page.IsPostBack = False Then
Session("my_name") = "Dave"
End If
Label1.Text = CStr(Session("my_name"))
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Session("my_name") = TextBox1.Text
End Sub
End Class
I set a session object at one juncture in my code:
Session("my_name") = "Dave"
Later in my code I give the user a chance to update this object:
Session("my_name") = TextBox1.Text
I reload my page and display a little hello statement like this:
Label1.Text = "Hello" & CStr(Session("my_name"))
The result is: "Hello Dave" no matter what I change Session("my_name") too.
EDIT: Here is the a full code-behind I wrote up to demonstrated:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1)
If Page.IsPostBack = False Then
Session("my_name") = "Dave"
End If
Label1.Text = CStr(Session("my_name"))
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Session("my_name") = TextBox1.Text
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Page
的Load
事件比Button
的 click 事件更快触发。因此,在运行时,Session("my_name")
的值仍然是“Dave”。如果您想正确设置它,您应该将
Label1.Text = CStr(Session("my_name"))
放入您的PreRender
事件处理程序中页。您也将其放入
Button
的Click
事件中(当然是在设置会话值之后),但我猜您想稍后使用该会话出于不那么琐碎的目的存储对象。(我猜您稍后想将会话用于更高级的目的。毕竟,如果您只想更改标签的文本,那么使用会话有什么意义?)
基本上,这就是您想要的:
以下是当前代码发生的情况:
您可以阅读更多内容有关该主题的信息,请参阅此处:ASP.NET 页面生命周期概述。
The
Page
'sLoad
event fires up sooner than theButton
's click event. Therefore, at the time it runs, the value ofSession("my_name")
is still "Dave".If you'd like to set it up correctly, you should either put the
Label1.Text = CStr(Session("my_name"))
into thePreRender
event handler of your page.You cut put it into the
Button
'sClick
event as well (after setting the session value, of course), but I guess that you want to use the session later for storing objects for less trivial purposes.(I guess that you'd like to use the session for more advanced purposes later. After all, what would be the point of using session if you only want to change a label's text?)
Basically, here is what you want:
Here's what's happening with your current code:
You can read more about the topic in here: ASP.NET Page Life Cycle Overview.
Page.Load
在您的Button1_Click
之前运行 - 因此您将文本框的值设置为会话中的任何值,然后稍后获取该文本框的内容(您已经覆盖了)并将其弹出回会话中。The
Page.Load
runs before yourButton1_Click
- so you are setting the value of your textbox to whatever you have in session, and then a bit later taking the contents of that textbox (which you have already overwritten) and popping it back into session.您没有正确设置会话变量默认值。基本上,您在每个页面加载(不是回发)时将会话变量设置为“Dave”。这甚至包括回调和页面重新加载。
要设置会话默认尝试...
现在您可以使用会话变量,而无需测试它是否是回发或回调的一部分。
You're not setting your Session variable default properly. Basically you're setting the session variable to 'Dave' on every page load that's not a postback. That even includes callbacks and page reloads.
To set the Session default try...
Now you can use your session variable without testing if it's part of a Postback or Callback.
这对我有用:
here's what worked for me: