简体   繁体   中英

Session not working in ASP.Net

I am new to ASP.Net and I can't seem grasp the working of my session. In some situations it works and in others not. Here you see the code where I create the session in one webform and call it in another form. In the Form_Load the second form can get data from the session but in the button_click event (page reload) the Session is "null". Can somebody help me?

code Form 1:

protected void Button1_Click(object sender, EventArgs e)
{
    Session["data"] = TextBox.Text;
    Response.Redirect("~/RapportFormulier2.aspx", false);
}

code Form 2:

string s;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Logic logic = new Logic();
        logic.login(credentials);
        s = (string)(Session["data"]); // data is stored in the variable s
    }
}

protected void ButtonOk_Click(object sender, EventArgs e)
{      
    string test = s; //null (normally because the reload)
    string s2 = (string)(Session["data"]); //null idk why
}

I put the connection to my logic class in my postback since i have to re initiase it anyway.

I am also open to some tips, since it is obvious i am struggling in ways.

edit: I tried storing the first s in another session but even that i couldn't get back in the button_click...

Form 2 load:

Session["data2"] = s;

Form 2 button_click:

string s2 = (string)(Session["data2"]); // null

I don't know the way you took the project

If you are sure that the code you wrote is correct, you can delete or comment this line from the web.config file. (Under <system.web> )

<httpCookies httpOnlyCookies="true" requireSSL="true" lockItem="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM