Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ObjectDataSource.CacheExpirationPolicy Property

Gets or sets the cache expiration behavior that, when combined with the duration, describes the behavior of the cache that the data source control uses.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public virtual DataSourceCacheExpiry CacheExpirationPolicy { get; set; }

Property Value

Type: System.Web.UI.DataSourceCacheExpiry
One of the DataSourceCacheExpiry values. The default is Absolute.

The ObjectDataSource control supports data caching. While data is cached, calls to the Select method retrieve data from the cache rather than from the business object that the ObjectDataSource works with. When the cache expires, the Select method retrieves data from the business object, and then caches the data again.

The ObjectDataSource control automatically caches data when the EnableCaching property is set to true and the CacheDuration property is set to a value greater than 0, which indicates the number of seconds that the cache stores data before the cache entry is discarded. A value of 0 indicates an infinitely long cache period.

The cache is regulated by a combination of the duration and the CacheExpirationPolicy setting. If the CacheExpirationPolicy property is set to the Absolute value, the ObjectDataSource caches data on the first call to the Select method and holds it in memory for, at most, the amount of time that is specified by the CacheDuration property. The data might be released before the duration time, if the memory is needed. The cache is then refreshed during the next call to the Select method. If the CacheExpirationPolicy property is set to the Sliding value, the data source control caches data on the first call to the Select method, but resets the time window for which it holds the cache for each subsequent call to the Select method. The cache expires if there is no activity for a time that is equal to the CacheDuration property since the last call to the Select method.

This section contains two code examples. The first code example demonstrates how an ObjectDataSource object supports filtering and caching. The second code example demonstrates how to implement a wrapper method that returns a DataSet object to enable caching and filtering with an ObjectDataSource object.

The following code example demonstrates how an ObjectDataSource control supports filtering and caching. To enable filtering and caching, you must implement the method that retrieves data, which is identified by the SelectMethod property, to return the data as a DataSet object. In this example, caching is enabled because the EmployeeLogic object returns data as a DataSet, the EnableCaching property is set to true, and the CacheDuration and CacheExpirationPolicy properties are set. The ObjectDataSource caches data that is returned by the SelectMethod property for 30 seconds.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>ObjectDataSource - C# Example</title>
    <form id="Form1" method="post" runat="server">

          datasourceid="ObjectDataSource1" />

          cacheexpirationpolicy="Absolute" />


The following code example demonstrates how to implement a wrapper method that returns a DataSet object to enable caching and filtering with an ObjectDataSource control. In the base implementation of the EmployeeLogic class, the GetAllEmployees method returns an ArrayList. Instead of refactoring the object completely to work with the ObjectDataSource on a Web Forms page, a wrapper method named GetAllEmployeesAsDataSet is added that returns a set of NorthwindEmployee data as a DataSet. This code example is part of a larger example provided for the ObjectDataSource class.

// To support basic filtering, the employees cannot 
// be returned as an array of objects, rather as a 
// DataSet of the raw data values. 
public static DataSet GetAllEmployeesAsDataSet () {
  ICollection employees = GetAllEmployees();

  DataSet ds = new DataSet("Table");

  // Create the schema of the DataTable.
  DataTable dt = new DataTable();
  DataColumn dc;
  dc = new DataColumn("FirstName", typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("LastName",  typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("Title",     typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("Courtesy",  typeof(string)); dt.Columns.Add(dc);
  dc = new DataColumn("Supervisor",typeof(Int32));  dt.Columns.Add(dc);

  // Add rows to the DataTable.
  IEnumerator emplEnum = employees.GetEnumerator();
  DataRow row;
  NorthwindEmployee ne;
  while (emplEnum.MoveNext()) {
    ne = emplEnum.Current as NorthwindEmployee;
    row = dt.NewRow();
    row["FirstName"]  = ne.FirstName;
    row["LastName"]   = ne.LastName;
    row["Title"]      = ne.Title;
    row["Courtesy"]   = ne.Courtesy;
    row["Supervisor"] = ne.Supervisor;
  // Add the complete DataTable to the DataSet.

  return ds;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
© 2015 Microsoft