ASP.NET实现Cache浏览器的管理

发表于:2007-06-30来源:作者:点击数: 标签:
对Cache进行浏览并管理。 %@ Page Language="a href="http://dev.21tx.com/dotnet/csharp/" target="_blank"C#/a" EnableViewState = "true"% %@ import Namespace="System" % %@ import Namespace="System.Configuration" % %@ import Namespace="System.Col
对Cache进行浏览并管理。
  
  <%@ Page Language="<a href="http://dev.21tx.com/dotnet/csharp/" target="_blank">C#</a>" EnableViewState = "true"%>
  <%@ import Namespace="System" %>
  <%@ import Namespace="System.Configuration" %>
  <%@ import Namespace="System.Collections" %>
  <%@ import Namespace="System.Collections.Specialized" %>
  <%@ import Namespace="System.Data" %>
  <script runat="server">
  //http://<a href="http://dev.21tx.com/web/asp/" target="_blank">ASP</a>alliance.com/aldotnet/examples/cacheviewer.aspx
  //http://scottwater.com
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
   hlRefresh.NavigateUrl = Request.RawUrl;
   BindGrid();
   }
  }
  
  //绑定到<a href="http://dev.21tx.com/dotnet/aspnet/datagrid/" target="_blank">DataGrid</a>
  private void BindGrid()
  {
   //创建arraylist来保存cacheditem信息
   ArrayList al = new ArrayList();
   IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
   while (CacheEnum.MoveNext())
   {
   al.Add(new CachedItem(CacheEnum.Key.ToString(),CacheEnum.Value.GetType().ToString()));
   }
   litCount.Text = al.Count.ToString();
   dgCachedItems.DataSource = al;
   dgCachedItems.DataBind();
  }
  
  //删除Cache项
  protected void Grid_ItemCommand(object sender, DataGridCommandEventArgs e)
  {
   Hy<a href="http://dev.21tx.com/web/perl/" target="_blank">Perl</a>ink l = (HyperLink)e.Item.FindControl("CacheItemName");
   //确保我们找到了该项
   if(l != null)
   {
   Cache.Remove(l.Text);
   }
   this.BindGrid();
  }
  
  protected void Grid_Created(object sender, DataGridItemEventArgs e)
  {
   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
   ((Literal)e.Item.FindControl("Counter")).Text = (e.Item.ItemIndex + 1).ToString();
   }
  }
  private class CachedItem
  {
   public CachedItem(){}
   public CachedItem(string key, string type)
   {
   this.CacheKey = key;
   this.CacheType = type;
   }
  
   private string _cacheKey;
   public string CacheKey
   {
   get {return this._cacheKey;}
   set {this._cacheKey = value;}
   }
  
   private string _cacheType;
   public string CacheType
   {
   get {return this._cacheType;}
   set {this._cacheType = value;}
   }
  }
  
  //Remove All
  void lbRemoveAll_Click(object sender, EventArgs e)
  {
   IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
   while (CacheEnum.MoveNext())
   {
   Cache.Remove(CacheEnum.Key.ToString());
   }
   BindGrid();
  }
  
  </script>
  <html>
  <head>
  <title>Cache管理器</title>
  <style>
   TD,A,P,SPAN {font-size:9pt;}
   .Header {background-color:#EDEDED;text-align:center;font-weight:bold;}
  </style>
  </head>
  <body >
  <form runat="server">
  浏览、删除缓存
  <br/>
  数量:
  <asp:Literal id="litCount" runat="server"></asp:Literal>
  <br/>
  <asp:LinkButton id="lbRemoveAll" onclick="lbRemoveAll_Click" runat="server">清除全部缓存项</asp:LinkButton>
  <br/>
  <asp:HyperLink id="hlRefresh" Text="刷新" Runat="server"></asp:HyperLink>
  <br/><br/>
  <asp:DataGrid id="dgCachedItems" runat="server" OnItemCommand="Grid_ItemCommand"
   AutoGenerateColumns="False" OnItemCreated="Grid_Created">
   <HeaderStyle CssClass="Header"></HeaderStyle>
   <Columns>
   <asp:TemplateColumn HeaderText="数量">
   <ItemTemplate>
   <asp:Literal id="Counter" runat="server" />
   </ItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="键">
   <ItemTemplate>
   <asp:HyperLink id="CacheItemName" runat="server" Target="_blank"
   Text=@#<%# ((CachedItem)(Container.DataItem)).CacheKey %>@#
   NavigateUrl = @#<%# ((CachedItem)(Container.DataItem)).CacheKey %>@#/>
   </ItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="类型">
   <ItemTemplate>
   <asp:Literal id="CacheItemDataType" runat="server"
   text=@#<%# ((CachedItem)(Container.DataItem)).CacheType %>@# />
   </ItemTemplate>
   </asp:TemplateColumn>
   <asp:TemplateColumn HeaderText="删除">
   <ItemTemplate>
   <asp:LinkButton ID="RemoveButton" Text="删除" CommandName="RemoveFromCache"
   runat="server" />
   </ItemTemplate>
   </asp:TemplateColumn>
   </Columns>
  </asp:DataGrid>
  </form>
  </body>
  </html>
  
  说明:DataGrid中用到了HyperLink Web控件,是为了方便本站的管理,其它则不必这样用。

原文转自:http://www.ltesting.net