<%@ 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;
using System.Xml;
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();
CacheDependency fileDepends = new CacheDependency(Server.MapPath("NorthWind.xml"));
Cache.Insert("GridViewDataSet", dsGrid, fileDepends);
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("NorthWind.xml"));
dsData = doc.DataSet;
return dsData;
}
protected void btnClear_Click(object sender, EventArgs e)
{
Cache.Remove("GridViewDataSet");
CreateGridView();
}
}
File: NorthWind.xml