ObjectDataSource.EnableCaching Property
Assembly: System.Web (in system.web.dll)
/** @property */ public boolean get_EnableCaching () /** @property */ public void set_EnableCaching (boolean value)
public function get EnableCaching () : boolean public function set EnableCaching (value : boolean)
Not applicable.
Property Value
true if data caching is enabled for the data source control; otherwise, false. The default is false.| Exception type | Condition |
|---|---|
| The EnableCaching property is set to true when the method specified by the SelectMethod property returns a DbDataReader. |
The ObjectDataSource control supports data caching. While data is cached, calls to the Select method retrieve data from the cache rather than the ObjectDataSource creating an instance of the business object and calling its data method. When the cache expires, the Select method retrieves data from the business object, and then caches the data again.
Note: |
|---|
| If you are not familiar with the Data Access features in ASP.NET 2.0, you should read some of these topics before continuing: |
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.
This section contains two code examples. The first code example demonstrates how an ObjectDataSource object supports caching. The second code example demonstrates how to implement a wrapper method that returns a DataSet object to enable caching with an ObjectDataSource object.
The following code example demonstrates how an ObjectDataSource control supports caching. To enable caching, you must implement the method that retrieves data, which is identified by the SelectMethod property, to return the data as a DataSet. In this example, caching is enabled because the EmployeeLogic object returns data as a DataSet object, 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.jsl" Assembly="Samples.AspNet.jsl" %>
<%@ Page Language="VJ#" %>
<!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>
<title>ObjectDataSource - VJ# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1" />
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
typename="Samples.AspNet.jsl.EmployeeLogic"
selectmethod="GetAllEmployeesAsDataSet"
enablecaching="True"
cacheduration="30"
cacheexpirationpolicy="Absolute" />
</form>
</body>
</html>
The following code example demonstrates how to implement a wrapper method that returns a DataSet object to enable caching with an ObjectDataSource control. In the base implementation of the EmployeeLogic class, the GetAllEmployees method returns an ArrayList object. 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() throws NorthwindDataException
{
ICollection employees = GetAllEmployees();
DataSet ds = new DataSet("Table");
// Create the schema of the DataTable.
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn("FirstName", String.class.ToType());
dt.get_Columns().Add(dc);
dc = new DataColumn("LastName", String.class.ToType());
dt.get_Columns().Add(dc);
dc = new DataColumn("Title", String.class.ToType());
dt.get_Columns().Add(dc);
dc = new DataColumn("Courtesy", String.class.ToType());
dt.get_Columns().Add(dc);
dc = new DataColumn("Supervisor", Int32.class.ToType());
dt.get_Columns().Add(dc);
// Add rows to the DataTable.
IEnumerator emplEnum = employees.GetEnumerator();
DataRow row;
NorthwindEmployee ne;
while (emplEnum.MoveNext()) {
ne = (NorthwindEmployee)emplEnum.get_Current();
row = dt.NewRow();
row.set_Item("FirstName", ne.get_FirstName());
row.set_Item("LastName", ne.get_LastName());
row.set_Item("Title", ne.get_Title());
row.set_Item("Courtesy", ne.get_Courtesy());
row.set_Item("Supervisor", (Int32)ne.get_Supervisor());
dt.get_Rows().Add(row);
}
// Add the complete DataTable to the DataSet.
ds.get_Tables().Add(dt);
return ds;
} //GetAllEmployeesAsDataSet
Reference
ObjectDataSource ClassObjectDataSource Members
System.Web.UI.WebControls Namespace
ObjectDataSource.CacheDuration Property
ObjectDataSource.CacheExpirationPolicy Property
Other Resources
ASP.NET Data Access OverviewData Source Web Server Controls
ObjectDataSource Control Overview
Creating an ObjectDataSource Control Source Object
Note: