XmlDataSource.EnableCaching 屬性

定義

取得或設定值,指出 XmlDataSource 控制項是否啟用了資料快取。Gets or sets a value indicating whether the XmlDataSource control has data caching enabled.

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,否則為 falsetrue if data caching is enabled for the data source control; otherwise, false. 預設值是 trueThe default value is true.

範例

下列程式碼範例示範如何在使用 XmlDataSource 控制項來顯示 XML 檔案中包含的資料時,啟用快取。The following code example demonstrates how to enable caching when using the XmlDataSource control to display data contained in an XML file. EnableCaching 屬性設定為 true ,且 CacheDuration 設定為數據源控制項快取資料的秒數時,即會啟用快取。Caching is enabled when the EnableCaching property is set to true and the CacheDuration is set to the number of seconds that the data is cached by the data source control.

<%@ 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 檔案包含下列資料:The XML file in the code example has the following data:

<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>  

備註

XmlDataSource當屬性設定為時,控制項會自動快取資料 EnableCaching trueThe XmlDataSource control automatically caches data when the EnableCaching property is set to true. 根據預設, CacheDuration 屬性會設定為0,表示不限數量的快取,而且資料來源會快取資料,直到其相依的 XML 檔案變更為止。By default, the CacheDuration property is set to 0, which indicates an indefinite cache, and the data source will cache data until the XML file that it depends on is changed. 如果您將 CacheDuration 屬性設定為大於0的值,則快取會在抓取新的資料集之前,儲存該秒數的資料。If you set the CacheDuration property to a value greater than 0, the cache stores data for that many seconds before retrieving a fresh set.

如果 EnableCaching 屬性設定為, trueTransformArgumentList 已設定屬性,則當轉換引數清單中的參數變更時,快取專案不會自動失效。If the EnableCaching property is set to true and the TransformArgumentList property is set, the cache entries are not automatically invalidated when the parameters change in the transforms argument list. 在這種情況下,您必須撰寫程式碼,藉由設定屬性使快取失效 CacheKeyDependencyIn that case, you must write code to invalidate the cache by setting the CacheKeyDependency property.

適用於

另請參閱