会话在 ASP.Net 中不起作用 [英] Session not working in ASP.Net

查看:47
本文介绍了会话在 ASP.Net 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ASP.Net 的新手,我似乎无法掌握会话的工作.在某些情况下它有效,而在其他情况下则无效.在这里,您可以看到我在一个网络表单中创建会话并在另一种表单中调用它的代码.在 Form_Load 中,第二个表单可以从会话中获取数据,但在 button_click 事件(页面重新加载)中,会话为空".有人可以帮我吗?

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?

代码形式 1:

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

代码形式 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.

我尝试将第一个 s 存储在另一个会话中,但即使如此我也无法返回 button_click...

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

Form 2 加载:

Session["data2"] = s;

表单 2 button_click:

Form 2 button_click:

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

推荐答案

我不知道你是怎么接项目的

I don't know the way you took the project

如果您确定您编写的代码是正确的,您可以从 web.config 文件中删除或注释此行.(在 下)

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" />

这篇关于会话在 ASP.Net 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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