在刷新页面之前,网站不会看到最新的会话值,但刷新会重置控件的值

时间:2022-02-08 00:49:23

My ASP.NET website doesn't see newest Session values. When my form is being sent to the server after clicking the button, the result sent back to browser depends of Session values. I process them in Page_Load method. It doesn't see the last change od Session values, but one before last. It looks like button's event handlers are executed before page's event handles but I'm not sure if it is like that. I tried to do my code in other methods like Page_PreInit etc, but it's still the same like that. The only thing that works for me is refreshing the page: Response.Redirect(Request.Url.AbsoluteUri); after any change of any Session value, but it resets values of all controls, which I want to be the same as before. Is there a better solution?

我的ASP.NET网站没有看到最新的会话值。单击按钮后将表单发送到服务器时,发送回浏览器的结果取决于会话值。我在Page_Load方法中处理它们。它没有看到会话值的最后一个变化,而是最后一个变化。看起来按钮的事件处理程序是在页面事件处理之前执行的,但我不确定它是否就是这样。我尝试在其他方法(如Page_PreInit等)中执行我的代码,但它仍然是这样的。唯一对我有用的是刷新页面:Response.Redirect(Request.Url.AbsoluteUri);在任何Session值的任何更改之后,它会重置所有控件的值,我希望它们与之前相同。有更好的解决方案吗?

Example: when It runs first time, the label's text is "Click the button", but when I click any of the buttons one time, nothing happens. When I click any of the button second time, label's text is the value of first click (even if I click A and then B, value changes to A after clicking B).

示例:当它第一次运行时,标签的文本是“单击按钮”,但是当我单击任何一个按钮时,没有任何反应。当我第二次单击任何按钮时,标签的文本是第一次单击的值(即使我单击A然后B,单击B后值更改为A)。

form:

形成:

<form id="form1" runat="server">
    <div>
        <p>
            <asp:Label ID="Label1" runat="server" />
        </p>
        <p>
            <asp:Button ID="Button1" runat="server" Text="A" onclick="Button1_Click" />&nbsp;
            <asp:Button ID="Button2" runat="server" Text="B" onclick="Button2_Click" />
        </p>
        <p>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </p>
    </div>
</form>

Event handlers:

事件处理程序:

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["Letter"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["Letter"] = "A";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["Letter"] = "B";
    }

Global method:

全球方法:

void Session_Start(object sender, EventArgs e) 
{
    Session["Letter"] = "Click the button";
}

1 个解决方案

#1


3  

Without your code its very difficult, but is sounds like you are setting a Session collection value in the code of a button's click event( or some other control/event ), and expecting it to be in the SESSION collection during the Page_Load event.

没有你的代码非常困难,但听起来你是在按钮的click事件(或其他一些控件/事件)的代码中设置Session集合值,并期望它在Page_Load事件期间在SESSION集合中。

It doesn't work like that - When the page request comes in, Page_Load happens before the control's events.

它不起作用 - 当页面请求进入时,Page_Load发生在控件的事件之前。

Instead of Page_Load use Page_PreRender this event occurs before the page is prepared to be sent back to the client.

而不是Page_Load使用Page_PreRender这个事件发生在页面准备发送回客户端之前。

Your addition of the code confirms the above.

您添加的代码确认了上述内容。

Normally I wouldn't use Session_Start to initialize stuff like this, use Page_Load and IsPostBack property

通常我不会使用Session_Start来初始化这样的东西,使用Page_Load和IsPostBack属性

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
       Label1.Text = "Click the button";
    }
}

#1


3  

Without your code its very difficult, but is sounds like you are setting a Session collection value in the code of a button's click event( or some other control/event ), and expecting it to be in the SESSION collection during the Page_Load event.

没有你的代码非常困难,但听起来你是在按钮的click事件(或其他一些控件/事件)的代码中设置Session集合值,并期望它在Page_Load事件期间在SESSION集合中。

It doesn't work like that - When the page request comes in, Page_Load happens before the control's events.

它不起作用 - 当页面请求进入时,Page_Load发生在控件的事件之前。

Instead of Page_Load use Page_PreRender this event occurs before the page is prepared to be sent back to the client.

而不是Page_Load使用Page_PreRender这个事件发生在页面准备发送回客户端之前。

Your addition of the code confirms the above.

您添加的代码确认了上述内容。

Normally I wouldn't use Session_Start to initialize stuff like this, use Page_Load and IsPostBack property

通常我不会使用Session_Start来初始化这样的东西,使用Page_Load和IsPostBack属性

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
       Label1.Text = "Click the button";
    }
}