session变量的问题
VS2005中.做了一个母版, 在母版中加入:
if (Session[ "name "].ToString() == " " || Session[ "postition "].ToString() == " ")
{
Response.Write( " <script> alert( '登陆信息过期!请重新登陆! ');document.location= '~/Login.aspx ' </script> ");
}
Session.Timeout = 5;
然后应用到每一页.
为什么第一次运行~打开某个页面,不弹出此消息.非得要注销次~将Session清掉才行?
怎么解决?
------解决方案--------------------用 page.ClientScript.RegisterClientScriptBlock来做
public static void ShowMessageAndRedirect(string errmsg, System.Web.UI.Page page, string url)
{
if (!page.ClientScript.IsClientScriptBlockRegistered( "MessageBox "))
{
string script = " <script language= 'javascript '> top.alert( ' " + errmsg + " ');top.location.href= ' "+url+ " '; </script> ";
page.ClientScript.RegisterClientScriptBlock(typeof(string), "MessageBox ", script);
}
}
------解决方案--------------------if (Session[ "name "].ToString() == " " || Session[ "postition "].ToString() == " ")
这样才是Session 丢失
if (Session[ "name "].ToString() == null || Session[ "postition "].ToString() == null)
------解决方案--------------------if (Session[ "name "] == null || Session[ "postition "] == null)
注销的时候执行session[ "name "]=null;或session.remove( "name ");
------解决方案--------------------if (Session[ "name "].ToString() == " " || Session[ "postition "].ToString() == " ")
{
Response.Write( " <script> alert( '登陆信息过期!请重新登陆! ');document.location= '~/Login.aspx ' </script> ");
}
Session.Timeout = 5;
================================
这么写。。。。。会出问题的Session[ "name "]==null要这样
------解决方案--------------------if (Session[ "name "].ToString() == null || Session[ "postition "].ToString() == null)
{
Response.Write( " <script> alert( '登陆信息过期!请重新登陆! ');document.location= '~/Login.aspx ' </script> ");
}
Session.Timeout = 5;
------解决方案--------------------楼上的方法是错的...
都为NULL了还怎么ToString()..........
if (Session[ "name "] == null || Session[ "postition "] == null)
{
Response.Write( " <script> alert( '登陆信息过期!请重新登陆! ');document.location= '~/Login.aspx ' </script> ");
}
Session.Timeout = 5;
------解决方案--------------------