2017-06-01 50 views
2

我尝试添加会话,然后我另一个页面上访问重定向后,这始终显示为空重定向到另一个页面时,会话始终为空? C#

Session.Add("pUser_Name", ds_1.Tables[0].Rows[0]["User_Name"].ToString()); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
Response.Redirect("DashBoard.aspx", false); 

这里另一页上我正在访问这样

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Session["pUser_Name"] == null) ////here is error NULL 
    { 

     Response.Redirect("Admin.aspx"); 
    } 
} 

我试图

protected override void OnInit(EventArgs e) 
{ 
    if (Session["pUser_Name"] == null) 
    { 
     Response.Redirect("Admin.aspx"); 
    } 
    base.OnInit(e); 
} 

还有

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DashBoard.aspx.cs" Inherits="SC.DashBoard" EnableTheming="true" 
Theme="Theme1" EnableSessionState="True" %> 

和管理页面,价值分配到会话

<%@ Page Language="C#" AutoEventWireup="true" EnableSessionState="True" CodeBehind="Admin.aspx.cs" Inherits="SC.Admin" %> 

和web.config文件

<system.web> 
    <compilation debug="true" targetFramework="4.0"/> 
    <sessionState timeout="12000"/> 
</system.web> 

,但仍面临着相同的会话是空

+0

只是一个尝试,而不是'Session.Add'使用'HttpContext.Current.Session.Add'和访问像'HttpContext.Current.Session [“pUser_Name”]' –

+0

同样的问题仍然没有解决 –

+0

更新面板也没有解决问题.. –

回答

0

在此基础上answer基本上指这msdn articleSessionID,您可能需要初始化您的会话对象Session_Start

protected void Session_Start(Object sender, EventArgs e) 
{ 
    Session["init"] = 0; 
} 

当会话状态存储在cookie中时,这是必需的,您当前使用的cookie基于web.config。原因在于ASP.NET在第一次使用之前不会保存会话,因此会在每个新页面页面请求中生成新的SessionID,当您将Response.Redirect发送到另一个页面时会发生这种情况。

相关问题