共用方式為


XmlDataSource.EnableCaching 屬性

定義

取得或設定值,指出 XmlDataSource 控制項是否啟用了資料快取。

public:
 virtual property bool EnableCaching { bool get(); void set(bool value); };
public virtual bool EnableCaching { get; set; }
member this.EnableCaching : bool with get, set
Public Overridable Property EnableCaching As Boolean

屬性值

Boolean

如果啟用了資料來源控制項的資料快取則為 true,否則為 false。 預設值是 true

範例

下列程式碼範例示範如何使用 控制項來顯示 XML 檔案中包含的資料時 XmlDataSource ,啟用快取。 當 EnableCaching 屬性設定 true 為 ,且 CacheDuration 設定為數據源控制項快取資料的秒數時,就會啟用快取。

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

    <form id="form1" runat="server">
      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml"
        enablecaching="True"
        cacheduration="60"
        cacheexpirationpolicy="Sliding" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:treeview>

    </form>
  </body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <asp:xmldatasource
        id="XmlDataSource1"
        runat="server"
        datafile="books.xml"
        enablecaching="True"
        cacheduration="60"
        cacheexpirationPolicy="Sliding" />

      <!- TreeView uses hierachical data, so the
          XmlDataSource uses an XmlHierarchicalDataSourceView
          when a TreeView is bound to it. -->

      <asp:treeview
        id="TreeView1"
        runat="server"
        datasourceid="XmlDataSource1">
        <databindings>
          <asp:treenodebinding datamember="book" textfield="title"/>
        </databindings>
      </asp:treeview>

    </form>
  </body>
</html>

程式碼範例中的 XML 檔案具有下列資料:

<books>  
   <computerbooks>  
     <book title="Secrets of Silicon Valley" author="Sheryl Hunter"/>  
     <book title="Straight Talk About Computers" author="Dean Straight"/>  
     <book title="You Can Combat Computer Stress!" author="Marjorie Green"/>                  
   </computerbooks>  
   <cookbooks>  
     <book title="Silicon Valley Gastronomic Treats" author="Innes del Castill"/>  
   </cookbooks>  
</books>  

備註

當 屬性設定為 true 時, EnableCaching 控制項 XmlDataSource 會自動快取資料。 根據預設, CacheDuration 屬性會設定為 0,這表示無限快取,而且資料來源會快取資料,直到它相依的 XML 檔案變更為止。 如果您將 CacheDuration 屬性設定為大於 0 的值,快取會在擷取新集合之前儲存該秒的資料。

EnableCaching如果屬性設定 true 為 ,而且 TransformArgumentList 已設定 屬性,當轉換引數清單中的參數變更時,快取專案不會自動失效。 在此情況下,您必須撰寫程式碼,藉由設定 CacheKeyDependency 屬性來使快取失效。

適用於

另請參閱