ASP.NET網站管理系統退出 清除瀏覽器緩衝,Session的代碼

來源:互聯網
上載者:User

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" <SPAN style="BACKGROUND-COLOR: #ff0000">runat="server" onserverclick</SPAN> ="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.jb51.net/article/30313.htm
剛開始前台代碼用的是(主版頁面前台代碼): 複製代碼 代碼如下:<a href="~/Login.aspx" class="atop" target="_self" onclick=“clear()”>賬戶資訊</a>
<script>
  function clear()<BR>  {<BR>    <%Clear();%><BR>  }
</script>

主版頁面後台代碼 複製代碼 代碼如下:public void Clear()
{
Session["id"] = null;
Session["name"] = null;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.