管理系统退出登陆后,将网址重新输入后还会看到用户登陆后的界面,为了解决这个问题,我采用了以下方法:

1、在系统登陆成功时记录登陆的用户名、密码等信息(登陆功能的部分代码)

                 Session["id"] = user.id.ToString();
                Session["name"] = user.name.ToString();
                Session["pwd"] = user.password.ToString();
                Session["time"] = user.LoginTime.ToString();
                Session["authority"] = user.limits.ToString();

2、在管理系统的每个页面中加入以下代码,在页面加载时判断session的值是否为空

protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["id"] == null || Session["name"] == null || Session["time"] == null || Session["authority"] == null || Session["pwd"] == null)
                Response.Redirect("~/Login.aspx", true);
            if (!IsPostBack)
            {
                。。。。。。
            }
        }

 3、在点击“退出系统”执行的事件中加入session清空的代码和浏览器缓存清空的代码

 public void Clear(object sender, EventArgs e)

        {            
            Session["id"] = null;
            Session["name"] = null;
            ClearClientPageCache();
            Response.Redirect("~/Login.aspx");
        }
        public  void ClearClientPageCache()
        {
            //清除浏览器缓存
            Response.Buffer = true;    
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);    
            Response.Cache.SetExpires(DateTime.Now.AddDays(-1));    
            Response.Expires = 0;    
            Response.CacheControl = "no-cache";    
            Response.Cache.SetNoStore(); 

        } 

 由于我的“退出系统”时用HTML的<a>标签写在母版页中的,因此上面的代码是写在母版页的.cs文件中的。

母版页代码:

<a  class="atop" target="_self" runat="server"  onserverclick ="Clear"  >退出系统</a>

=========================================================================================== 

之前的版本一直不能实现功能,纠结了很久,没有找出问题所在,把刚开始执行错误的代码贴出来,同时也我把自己的认为错误的地方贴出来,希望大家批评指正。

这个版本的错误是:登陆成功后进入主页面,然后再点击进入其他页面时都无法进入,都会跳到登陆界面。

 

我的思考:

      1、我在跟踪调试时发现,每次页面加载时都会自动执行母版页.cs文件中的Clear()方法,因此不能通过其他页面Page_Load()方法中 的 if (Session["id"] == null || Session["name"] == null || Session["time"] == null || Session["authority"] == null || Session["pwd"] == null)  Response.Redirect("~/Login.aspx", true);

      2、我的疑问在于,Clear()方法明明是点击后才执行的,为什么每次加载页面的时候都自动执行?

      3、我考虑错误的原因是客户端和服务器端执行方法的不同,然后在网上找了关于onclick,和onserverclick的区别,但是对他们的理解还不是很清楚。希望大家能够交流一下。

关于onclick,和onserverclick的区别参见:http://www.cnblogs.com/zuiyirenjian/archive/2009/11/26/1611003.html

 

刚开始前台代码用的是(母版页前台代码):

<a href="~/Login.aspx" class="atop" target="_self" onclick=“clear()”>退出系统</a>

<script>
  function clear()
  {
    <%Clear();%>
  } </script>

 母版页后台代码

public void Clear()

        {            
            Session["id"] = null;
            Session["name"] = null;
        }

 

posted on 2012-05-17 13:08  Miko2012  阅读(17525)  评论(2编辑  收藏  举报