清除会话变量值 [英] Clear session variable value
问题描述
我想在注销事件中清除会话变量值.但它行不通.
代码在这里
I want to clear session varibale value in logout event. but it won''t works.
code is here
if (Page.IsPostBack == false)
{
string s = Session["loginid"].ToString();
if (Session["loginid";].ToString() == "")
{
Response.Redirect("errorpage1.aspx");
}
}
推荐答案
通常,如果用户注销,我们使用Session.Abondon()
方法销毁存储在会话对象中的所有对象. 如果只需要清除特定的会话对象,请将其设置为null.
Usually we useSession.Abondon()
method to destroy all objects stored in session object if a user logs out.
If you need to clear only a particular session object, set it to null.
Session["loginid"] = null;
并在需要检查是否有任何值的地方进行空检查.
And do a null check wherever you need to check if it has any value or not.
if (Session["loginid"] == null)
{
//redirect
}
else
{
//user is logged in
}
Session["loginid"] = "";
仍然引用内存位置,但是如果为会话对象分配空值,它将不再引用任何内存位置,在您的情况下更有意义.
顺便说一句,如果(Session ["loginid" ; ].ToString()==")
,我还注意到您的代码中存在语法错误
分号不应该在那儿.
希望这会有所帮助!
Session["loginid"] = "";
still refers to a memory location, but if you assign the session object a null value, it will no more refer to any memory location, which in your case makes more sense.
BTW I also notice a syntax error in your code, if (Session["loginid";].ToString() == "")
The semi colon shouldn''t be there.
Hope this helps!
u可以像
那样做
u can do like
Session["loginid"]="";
这篇关于清除会话变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!