<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
Object Caching
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching; // necessary for CacheDependency
using System.Xml; // necessary for Xml stuff
public partial class Default_aspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateGridView();
}
private void CreateGridView()
{
DataSet dsGrid;
dsGrid = (DataSet)Cache["GridViewDataSet"];
if (dsGrid == null)
{
dsGrid = GetDataSet();
string[] fileDependsArray = {Server.MapPath("Data.xml")};
string[] cacheDependsArray = {"Depend0", "Depend1", "Depend2"};
CacheDependency cacheDepends = new CacheDependency
(fileDependsArray, cacheDependsArray);
Cache.Insert("GridViewDataSet", dsGrid, cacheDepends);
lblMessage.Text = "Data from XML file.";
}
else
{
lblMessage.Text = "Data from cache.";
}
gv.DataSource = dsGrid.Tables[0];
gv.DataBind();
}
private DataSet GetDataSet()
{
DataSet dsData = new DataSet();
XmlDataDocument doc = new XmlDataDocument();
doc.DataSet.ReadXml(Server.MapPath("Data.xml"));
dsData = doc.DataSet;
return dsData;
}
protected void btnClear_Click(object sender, EventArgs e)
{
Cache.Remove("GridViewDataSet");
CreateGridView();
}
protected void btnInit_Click(object sender, EventArgs e)
{
Cache["Depend0"] = "This is the first dependency.";
Cache["Depend1"] = "This is the 2nd dependency.";
Cache["Depend2"] = "This is the 3rd dependency.";
}
protected void btnKey0_Click(object sender, EventArgs e)
{
Cache["Depend0"] = "This is a changed first dependency.";
}
}
File: NorthWind.xml