Sessions.LoginName = cusName; 
Sessions.CustomerID = Convert.ToInt32(ds.Tables[0].Rows[0]["PKID"]);
public static class Sessions
    {
        private static HttpContext CurrContext
        {
            get { return HttpContext.Current; }
        }        /// <summary>
        /// 用户登陆后的用户ID,0表示没有登陆
        /// </summary>
        public static int CustomerID
        {
            get
            {
                try
                { return CurrContext.Session["CustomerID"] == null ? 0 : Convert.ToInt32(CurrContext.Session["CustomerID"]); }
                catch
                { return 0; }
            }
            set
            { CurrContext.Session["CustomerID"] = value; }
        }        /// <summary>
        /// 用户登陆后的用户登录名,""表示没有登陆
        /// </summary>
        public static string LoginName
        {
            get
            {
                return CurrContext.Session["LoginName"] == null ? "" : CurrContext.Session["LoginName"].ToString().Trim();
            }
            set
            { CurrContext.Session["LoginName"] = value; }
        }
} if (!Page.IsPostBack)
            {
                if (Sessions.LoginName.ToString() == "")
                {
                    Response.Redirect(Request.ApplicationPath.ToString() + "index.aspx", true);
                }
                else
                {
                    int pkid = Convert.ToInt32(Sessions.CustomerID.ToString());
                    FillInfo(pkid);
                }
            }
代码如上:为什么我在另一个页面(从登录页面跳转过去的页面)获取不到Sessions.LoginName呢?

解决方案 »

  1.   

    转一个别人的经验:最近做了一个项目发现 Response.Redirect 后 Session 会丢失,搞了两天终于发现问题所在。
    问题代码
    Session["xxx"] = xxx;
    Response.Redirect("yyy.aspx");当页面跳转到 yyy.aspx , Session 丢失,访问 Session[“xxx”] 得到 null.
    原因:当asp.net 执行 Response.Redirect 时会强制终止当前Response ,不发送当前页面的cookie 给浏览器,而是发送一个指令告诉浏览器重新发送一个新的HTTP请求到新的URL,结果导致当前的Session 丢失。
    解决这个问题的方法是在当前页面接受Get请求时就设置一下Session ,这样浏览器就会记录下当前的Session Id,当浏览器再发送Post 请求到当前页面时,Server就不需要再发送一次Session ID 的cookie 给浏览器了
      

  2.   

    是否使用了Global文件,查看在事件Application_BeginRequest中是否对session做了“手脚”
      

  3.   

    楼上说的真的还是假的?为什么我在用Response.Redirect()后还是可以收到Session的值,
    比如从logIn.aspx,页面到a.aspx,可以收到session的值,但是从a.aspx,到b.aspx页面就手不到session 的值了。
    我也一直没有搞清楚!
    但是我用application全局变量,就可以实现!
      

  4.   

    那你就要对比一下,login.aspx与a.aspx在跳转前的操作有什么不同.
      

  5.   

    我不知道Response.Redirect("yyy.aspx");会不会造成Session丢失
    但有一点是肯定的:
    asp.net 执行 Response.Redirect 时会强制终止当前Response解决的办法其实很简单,就是写成 Response.Redirect("yyy.aspx",false)大家去看看这个方法的第二个参数解释就知道了
      

  6.   

    if (Session["uname"]==null){
    Session["uname"]="uname";
    }
    else
    {
    string uname=Session["uname"].toString();
    }
      

  7.   

      说用cookie的  我晕 
        cookie和session 完全是两个 概念 LZ的代码确实很乱的   调试了吗   
    调试一下  
    看看是哪里出了问题啊  
      

  8.   

    把第二个aspx发上来要实现IHttpSessionState ,看看是不是这个原因
      

  9.   

    这位仁兄,有何高见呢? 对于这个Session丢失的问题?我跳转到其他页面没一点问题,跳到这就出现这问题了?
      

  10.   

    在这:
    if (!Page.IsPostBack) 

             if (Sessions.LoginName.ToString() == "") 
             { 
                   Response.Redirect(Request.ApplicationPath.ToString() + "index.aspx", true); 
             } 
             else 
             { 
                   int pkid = Convert.ToInt32(Sessions.CustomerID.ToString()); 
                   FillInfo(pkid); 
             } 
      

  11.   

    先把你自己包装的代码去掉。存的时候直接:
    Session["UserName"]="123"再取的时候直接:
    if(Session["UserName"]==null)

       Response.Redirect("index.aspx");
    }
    else
    {
      
    }试试看。 就知道是你代码的问题,还是丢失的问题。Response.Redirect导致vsession丢失?说实话,还是第一次听说。
      

  12.   

    好像Response.Redirect是在客户端执行的。直接重定向网页。楼主可以试试Server.Transfer