Editare

Partajați prin


ObjectDataSource.EnableCaching Property

Definition

Gets or sets a value indicating whether the ObjectDataSource 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

Property Value

true if data caching is enabled for the data source control; otherwise, false. The default is false.

Exceptions

The EnableCaching property is set to true when the method specified by the SelectMethod property returns a DbDataReader.

Examples

The following three examples show a Web page, a code-behind page class, and a data-access class that retrieve records from the Employees table in the Northwind database.

The first example shows a Web page that contains two ObjectDataSource controls, a DropDownList control, and a DetailsView control. The first ObjectDataSource control and the DropDownList control are used to retrieve and display employee names from the database. The second ObjectDataSource control and the DetailsView control are used to retrieve and display the employee record that is selected by the user.

Caching is enabled for the ObjectDataSource control. Therefore, each record is retrieved only one time from the database. The CacheKeyDependency property is set to "EmployeeDetails", but any string value can work as the key. The Web page also includes a Button control that the user can click to expire the cached data.

<form id="form1" runat="server">
<div>
<asp:objectdatasource
      ID="ObjectDataSource1"
      runat="server"
      SelectMethod="GetFullNamesAndIDs"
      TypeName="Samples.AspNet.CS.EmployeeLogic" />
      
    <p>
    <asp:dropdownlist
      ID="DropDownList1"
      runat="server" 
      DataSourceID="ObjectDataSource1"
      DataTextField="FullName"
      DataValueField="EmployeeID" 
      AutoPostBack="True" 
      AppendDataBoundItems="true">
        <asp:ListItem Text="Select One" Value=""></asp:ListItem>
    </asp:dropdownlist>
    </p>
    
 <asp:objectdatasource
      ID="ObjectDataSource2"
      runat="server"
      SelectMethod="GetEmployee"
      TypeName="Samples.AspNet.CS.EmployeeLogic" 
      EnableCaching="true"
      CacheKeyDependency="EmployeeDetails" >
      <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
      </SelectParameters>
    </asp:objectdatasource>
    
    <asp:DetailsView
        ID="DetailsView1"
        runat="server"
        DataSourceID="ObjectDataSource2" 
        AutoGenerateRows="false">  
        <Fields>
            <asp:BoundField HeaderText="Address" DataField="Address" />
            <asp:BoundField HeaderText="City" DataField="City" />
            <asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
        </Fields>  
    </asp:DetailsView>
    
    <asp:Button 
    ID="Button1" 
    runat="server" 
    Text="Check for latest data" 
    OnClick="Button1_Click" />
    
</div>
</form>
<form id="form1" runat="server">
<div>
<asp:objectdatasource
      ID="ObjectDataSource1"
      runat="server"
      SelectMethod="GetFullNamesAndIDs"
      TypeName="Samples.AspNet.CS.EmployeeLogic" />
      
    <p>
    <asp:dropdownlist
      ID="DropDownList1"
      runat="server" 
      DataSourceID="ObjectDataSource1"
      DataTextField="FullName"
      DataValueField="EmployeeID" 
      AutoPostBack="True" 
      AppendDataBoundItems="true">
        <asp:ListItem Text="Select One" Value=""></asp:ListItem>
    </asp:dropdownlist>
    </p>
    
 <asp:objectdatasource
      ID="ObjectDataSource2"
      runat="server"
      SelectMethod="GetEmployee"
      TypeName="Samples.AspNet.CS.EmployeeLogic" 
      EnableCaching="true"
      CacheKeyDependency="EmployeeDetails" >
      <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" DefaultValue="-1" Name="empID" />
      </SelectParameters>
    </asp:objectdatasource>
    
    <asp:DetailsView
        ID="DetailsView1"
        runat="server"
        DataSourceID="ObjectDataSource2" 
        AutoGenerateRows="false">  
        <Fields>
            <asp:BoundField HeaderText="Address" DataField="Address" />
            <asp:BoundField HeaderText="City" DataField="City" />
            <asp:BoundField HeaderText="Postal Code" DataField="PostalCode" />
        </Fields>  
    </asp:DetailsView>
    
    <asp:Button 
    ID="Button1" 
    runat="server" 
    Text="Check for latest data" 
    OnClick="Button1_Click" />
    
</div>
</form>

The second example shows a handler for the Load event and a handler for the Click event of the Button control. The Load event handler creates a cache item with a key set to the CacheKeyDependency value. The Click event handler removes the cache item whose key is equal to the CacheKeyDependency value. When the cache item is removed, all the cached data that is dependent on the key is expired.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Cache[ObjectDataSource2.CacheKeyDependency] = "CacheExample";
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Cache.Remove(ObjectDataSource2.CacheKeyDependency);
    Cache[ObjectDataSource2.CacheKeyDependency] = "CacheExample";
    DetailsView1.DataBind();
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not (IsPostBack) Then
        Cache(ObjectDataSource2.CacheKeyDependency) = "CacheExample"
    End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Cache.Remove(ObjectDataSource2.CacheKeyDependency)
    Cache(ObjectDataSource2.CacheKeyDependency) = "CacheExample"
    DetailsView1.DataBind()
End Sub

The third example shows the data access class that interacts with the Northwind database. The class uses LINQ to query the Employees table. The example requires a LINQ to SQL class that represents the Northwind database and the Employees table. For more information, see How to: Create LINQ to SQL Classes in a Web Project.

public class EmployeeLogic
{
    public static Array GetFullNamesAndIDs()
    {
        NorthwindDataContext ndc = new NorthwindDataContext();

        var employeeQuery =
            from e in ndc.Employees
            orderby e.LastName
            select new { FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID };

        return employeeQuery.ToArray();
    }

    public static Employee GetEmployee(int empID)
    {
        if (empID < 0)
        {
            return null;
        }
        else
        {
            NorthwindDataContext ndc = new NorthwindDataContext();
            var employeeQuery =
                from e in ndc.Employees
                where e.EmployeeID == empID
                select e;

            return employeeQuery.Single();
        }
    }
 
    public static void UpdateEmployeeAddress(Employee originalEmployee, string address, string city, string postalcode)
    {
        NorthwindDataContext ndc = new NorthwindDataContext();
        ndc.Employees.Attach(originalEmployee, false);
        originalEmployee.Address = address;
        originalEmployee.City = city;
        originalEmployee.PostalCode = postalcode;
        ndc.SubmitChanges();
    }
}
Public Class EmployeeLogic
    Public Shared Function GetFullNamesAndIDs() As Array
        Dim ndc As New NorthwindDataContext()

        Dim employeeQuery = _
            From e In ndc.Employees _
            Order By e.LastName _
            Select FullName = e.FirstName + " " + e.LastName, EmployeeID = e.EmployeeID

        Return employeeQuery.ToArray()
    End Function

    Public Shared Function GetEmployee(ByVal empID As Integer) As Employee

        If (empID < 0) Then
            Return Nothing
        Else
            Dim ndc As New NorthwindDataContext()
            Dim employeeQuery = _
                From e In ndc.Employees _
                Where e.EmployeeID = empID _
                Select e

            Return employeeQuery.Single()
        End If
    End Function

    Public Shared Sub UpdateEmployeeAddress(ByVal originalEmployee As Employee, ByVal address As String, ByVal city As String, ByVal postalcode As String)

        Dim ndc As New NorthwindDataContext()
        ndc.Employees.Attach(originalEmployee, False)
        originalEmployee.Address = address
        originalEmployee.City = city
        originalEmployee.PostalCode = postalcode
        ndc.SubmitChanges()
    End Sub
End Class

Remarks

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.

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.

Applies to

See also