2017-02-20 27 views
0

我正在使用會話。在第一頁我有代碼會話未在asp.net創建空引用異常

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

和歡迎頁面上我寫的形式加載事件

if(Session["me"] != null) 
{ 
    Label1.Text = (string)Session["me"]; 
} 
else 
{ 
    Label1.Text = "session not created"; 
} 

這段代碼它給我的「會話沒有創建」其他部分始終運行。我的代碼有問題。請幫幫我。

回答

2

你必須創建會話之前重定向到頁面,以便您的代碼必須 這樣的:

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

套裝sessionredirecting之前,其他頁面

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

使用

Session["me"] = TextBox1.Text; 

並在重定向之前使用它t o其他頁面

1

您必須先設置會話,然後重定向到其他頁面。所以它應該是這樣的:

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