繼上篇在套用 Master Page 下使用 OutputCache後,這篇就來寫一下如何清 OutputCache。
清除單頁 OutputCache 的方式可使用下列語法
1
| HttpResponse.RemoveOutputCacheItem("/test.aspx"); |
參考來源: http://www.dotblogs.com.tw/ajun/archive/2008/02/18/1078.aspx
但如果想要清除所有的 OutputCache,上網找到一個方法,實際測試是可以 WORK 的,以下為sample。
Global.asax
1
2
3
4
5
6
7
8
9
10
11
| <%@ Application Language="C#" %><script runat="server"> void Application_Start(object sender, EventArgs e) { HttpContext.Current.Cache.Insert("task-index", DateTime.Now); } </script> |
1
2
3
4
5
6
7
8
9
10
11
12
| <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><%@ OutputCache Location="Server" VaryByParam="*" Duration="60" %>... <form id="form1" runat="server"> <asp:label id="lblNow" runat="server"></asp:label> </form>... |
1
2
3
4
5
6
| protected void Page_Load(object sender, EventArgs e){ Response.AddCacheItemDependency("task-index"); lblNow.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");} |
1
2
3
4
5
6
7
8
| ...<asp:button id="btnClear" onclick="btnClear_Click" runat="server" text="Clear">...</asp:button> |
1
2
3
4
| protected void btnClear_Click(object sender, EventArgs e){ HttpContext.Current.Cache.Insert("task-index", DateTime.Now); } |
註:清除 OutputCache 的功能要放在另一頁,如果要做在同一頁可以使用 AJAX 技術。
沒有留言:
張貼留言