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.
结果是:“Hello Dave”无论我改变Session(“my_name”)也是如此。
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
3 个解决方案
#1
7
The Page
's Load
event fires up sooner than the Button
's click event. Therefore, at the time it runs, the value of Session("my_name")
is still "Dave".
Page的Load事件比Button的click事件更快启动。因此,在运行时,Session(“my_name”)的值仍然是“Dave”。
If you'd like to set it up correctly, you should either put the Label1.Text = CStr(Session("my_name"))
into the PreRender
event handler of your page.
如果您想正确设置它,您应该将Label1.Text = CStr(Session(“my_name”))放入页面的PreRender事件处理程序中。
You cut put it into the Button
's Click
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.
你也把它放入了Button的Click事件中(当然,在设置了会话值之后),但我想你以后想要使用会话来存储对象,以减少不重要的目的。
(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:
基本上,这就是你想要的:
- The Page_Load sets Session("my_name") to "Dave" if it is not a postback
- The Button1_Click sets Session("my_name") to the textbox's text
- The Page_PreRender sets the label's text.
如果不是回发,则Page_Load将Session(“my_name”)设置为“Dave”
Button1_Click将Session(“my_name”)设置为文本框的文本
Page_PreRender设置标签的文本。
Here's what's happening with your current code:
以下是您当前代码所发生的情况:
- The Page_Load sets Session("my_name") to "Dave" if it is not a postback
- The Page_Load sets the label's text
- The Button1_Click sets Session("my_name") to the textbox's text
如果不是回发,则Page_Load将Session(“my_name”)设置为“Dave”
Page_Load设置标签的文本
Button1_Click将Session(“my_name”)设置为文本框的文本
You can read more about the topic in here: ASP.NET Page Life Cycle Overview.
您可以在此处阅读有关该主题的更多信息:ASP.NET页面生命周期概述。
#2
3
The Page.Load
runs before your Button1_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.
Page.Load在您的Button1_Click之前运行 - 因此您将文本框的值设置为会话中的任何内容,然后稍后获取该文本框的内容(已经覆盖)并将其弹回到会话中。
#3
3
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.
您没有正确设置Session变量的默认值。基本上你是在每次页面加载时将会话变量设置为'Dave'而不是回发。这甚至包括回调和页面重新加载。
To set the Session default try...
要设置会话默认尝试...
if( String.IsNullOrEmpty(Session["my_name"]) )
{
Session["my_name"] = "Dave";
}
Now you can use your session variable without testing if it's part of a Postback or Callback.
现在,您可以使用会话变量而不进行测试,如果它是回发或回调的一部分。
#1
7
The Page
's Load
event fires up sooner than the Button
's click event. Therefore, at the time it runs, the value of Session("my_name")
is still "Dave".
Page的Load事件比Button的click事件更快启动。因此,在运行时,Session(“my_name”)的值仍然是“Dave”。
If you'd like to set it up correctly, you should either put the Label1.Text = CStr(Session("my_name"))
into the PreRender
event handler of your page.
如果您想正确设置它,您应该将Label1.Text = CStr(Session(“my_name”))放入页面的PreRender事件处理程序中。
You cut put it into the Button
's Click
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.
你也把它放入了Button的Click事件中(当然,在设置了会话值之后),但我想你以后想要使用会话来存储对象,以减少不重要的目的。
(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:
基本上,这就是你想要的:
- The Page_Load sets Session("my_name") to "Dave" if it is not a postback
- The Button1_Click sets Session("my_name") to the textbox's text
- The Page_PreRender sets the label's text.
如果不是回发,则Page_Load将Session(“my_name”)设置为“Dave”
Button1_Click将Session(“my_name”)设置为文本框的文本
Page_PreRender设置标签的文本。
Here's what's happening with your current code:
以下是您当前代码所发生的情况:
- The Page_Load sets Session("my_name") to "Dave" if it is not a postback
- The Page_Load sets the label's text
- The Button1_Click sets Session("my_name") to the textbox's text
如果不是回发,则Page_Load将Session(“my_name”)设置为“Dave”
Page_Load设置标签的文本
Button1_Click将Session(“my_name”)设置为文本框的文本
You can read more about the topic in here: ASP.NET Page Life Cycle Overview.
您可以在此处阅读有关该主题的更多信息:ASP.NET页面生命周期概述。
#2
3
The Page.Load
runs before your Button1_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.
Page.Load在您的Button1_Click之前运行 - 因此您将文本框的值设置为会话中的任何内容,然后稍后获取该文本框的内容(已经覆盖)并将其弹回到会话中。
#3
3
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.
您没有正确设置Session变量的默认值。基本上你是在每次页面加载时将会话变量设置为'Dave'而不是回发。这甚至包括回调和页面重新加载。
To set the Session default try...
要设置会话默认尝试...
if( String.IsNullOrEmpty(Session["my_name"]) )
{
Session["my_name"] = "Dave";
}
Now you can use your session variable without testing if it's part of a Postback or Callback.
现在,您可以使用会话变量而不进行测试,如果它是回发或回调的一部分。