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呢?
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呢?
问题代码
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 给浏览器了
比如从logIn.aspx,页面到a.aspx,可以收到session的值,但是从a.aspx,到b.aspx页面就手不到session 的值了。
我也一直没有搞清楚!
但是我用application全局变量,就可以实现!
但有一点是肯定的:
asp.net 执行 Response.Redirect 时会强制终止当前Response解决的办法其实很简单,就是写成 Response.Redirect("yyy.aspx",false)大家去看看这个方法的第二个参数解释就知道了
Session["uname"]="uname";
}
else
{
string uname=Session["uname"].toString();
}
cookie和session 完全是两个 概念 LZ的代码确实很乱的 调试了吗
调试一下
看看是哪里出了问题啊
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);
}
}
Session["UserName"]="123"再取的时候直接:
if(Session["UserName"]==null)
{
Response.Redirect("index.aspx");
}
else
{
}试试看。 就知道是你代码的问题,还是丢失的问题。Response.Redirect导致vsession丢失?说实话,还是第一次听说。