Implementing session data

2019-09-06 07:26发布

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:

Sub class:

public class invoiceHandler
{

    public HttpContext sessionContext = HttpContext.Current;

    ...
}

Page:

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);
    }
    ...
}

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

2条回答
Rolldiameter
2楼-- · 2019-09-06 07:41

In your saveButton_Click, you are using a variable invGrid. You probably think you have assigned this a value in updateactionsGridBind, but my guess is that that happened before you showed your page and clicked on your saveButton.

A postback (or rather any request) uses a new instance of your page class, so any variables have been reset (in this case to null). You need to get that value again from session.

查看更多
贼婆χ
3楼-- · 2019-09-06 07:52

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 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.

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

  1. Something causes session state to be discarded, such as IIS or the application pool being restarted
  2. Session state is disabled for the page, request, or application
  3. You are no longer on the same session.

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.)

查看更多
登录 后发表回答