会话未在asp.net中创建空引用异常

时间:2017-02-20 09:30:29

标签: c# asp.net session

我在做会话。在第一页我有代码

Response.Redirect("welcome.aspx");
Session["me"] = TextBox1.Text;

在欢迎页面上,我在表单加载事件

上编写此代码
if(Session["me"] != null)
{
  Label1.Text = (string)Session["me"];
}
else
{
  Label1.Text = "session not created";
}

它给了我“会话未创建”其他部分始终在运行。我的代码有问题。请帮帮我。

4 个答案:

答案 0 :(得分:2)

您必须在重定向到页面之前创建会话,因此您的代码必须是 像这样:

Session["me"] = TextBox1.Text;
    Response.Redirect("welcome.aspx");

答案 1 :(得分:1)

session之前将redirecting设置为其他页面

Session["me"] = TextBox1.Text;
Response.Redirect("welcome.aspx");

答案 2 :(得分:1)

使用

Session["me"] = TextBox1.Text;

并在重定向到其他页面之前使用它

答案 3 :(得分:1)

您必须先设置会话,然后重定向到其他页面。所以它应该是这样的:

Session["me"] = TextBox1.Text; // set the session
Response.Redirect("welcome.aspx"); // redirect to other page