实现会话数据 [英] Implementing session data

查看:42
本文介绍了实现会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 表单,可以从其默认网页中实例化一个类.该类实现会话数据,网页也是如此,但我正在努力使会话保持不变.

I have a web form that instantiates a class from within its default web page. The class implements session data, as does the web page, but I'm struggling to get the session to remain constant.

每次页面重新加载时,它都会重新实例化类,从而重新实例化会话,呈现我的会话的整个概念没有实际意义.

Each time the page reloads, it re-instantiates the class, and thus the session, rendering the entire concept of my session moot.

如何实现会话而不在页面本身或从页面实例化的任何类中实例化它?还是我的想法有误?

How does one implement a session without instantiating it in the page itself, or any classes that are instantiated from the page? Or have I got the wrong idea?

作为参考,这里是页面和子类.在你看到..."的地方省略了一些不必要的代码:

For reference, here is the page and sub-class. Where you see "..." some unnecessary code has been omitted:

子类:

public class invoiceHandler
{

    public HttpContext sessionContext = HttpContext.Current;

    ...
}

页面:

public partial class _Default : Page
{

Program.invoiceHandler handler = new Program.invoiceHandler();

...

protected void updateactionsGridBind()
    {
        handler.sessionContext = HttpContext.Current;
            try
            {
                invGrid.Columns[7].Visible = false;
                invGrid = (GridView)handler.sessionContext.Session["editaction"];

            ...
            }
        ...
    }

...

protected void saveButton_Click(object sender, EventArgs e)
    {

        handler.sessionContext = HttpContext.Current;
        invGrid.EditIndex = -1;
        string kinvConv = invGrid.Rows[0].Cells[0].Text;
        handler.sessionContext.Session["editaction"] = invGrid;
        tools.i = kinvConv;
        tools.ToInt();

        handler.sessionContext.Session["kinv"] = tools.j;
        handler.sessionContext.Session["<REDACTED>"] = invGrid.Rows[0].Cells[1].Text;
        handler.sessionContext.Session["sup"] = invGrid.Rows[0].Cells[2].Text;
        handler.sessionContext.Session["supn"] = invGrid.Rows[0].Cells[3].Text;
        handler.sessionContext.Session["net"] = invGrid.Rows[0].Cells[4].Text;
        handler.sessionContext.Session["vat"] = invGrid.Rows[0].Cells[5].Text;
        handler.sessionContext.Session["date"] = invGrid.Rows[0].Cells[6].Text;

        OnUpdate(sender, e);
    }
    ...
}

savebutton_click 处,会话似乎丢失了数据.

It is at the point of savebutton_click that the session appears to lose its data.

推荐答案

根据提供的代码示例,我不完全确定您是否理解 HttpContext.Current 有效.您似乎正在使用它来缓存上下文的当前状态.(我可能完全错了!)

Based on the code samples provided, I am not entirely certain that you understand how HttpContext.Current works. You appear to be using it to cache the current state of the context. (I could be completely wrong!)

HttpContext.Current 是一个静态值.创建一个代表它的属性除了创建它的快捷方式之外几乎没有什么作用.它肯定不会导致您在页面回发之间丢失状态.

HttpContext.Current is a static value. Creating a property that represents it achieves little besides creating a shortcut to it. It most certainly would not cause you to lose state between page postbacks.

同样,HttpContext.Current.Session 是一个静态值.因此,它不会在页面回发之间丢失值,除非:

Similarly, HttpContext.Current.Session is a static value. As such, it will not lose values between page postbacks, unless:

  1. 某些原因导致会话状态被丢弃,例如 IIS 或正在重新启动的应用程序池
  2. 页面、请求或应用程序的会话状态被禁用
  3. 您不再处于同一会话中.

检查以确认您没有禁用请求、页面或应用程序的会话状态.此外,请检查以验证您是否启用了 cookie.(会话状态通常通过使用 cookie 启用.)

Check to verify that you have not disabled session state for the request, the page, or the application. Also, check to verify that you have cookies enabled. (Session state is typically enabled through the use of a cookie.)

这篇关于实现会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆