After a lot of trial and error attempts I finally made it work. Let's go through the following code to demonstrate the technique.
Add DataSource objects to the page.
Enable cache for the DataSource object:
.... asp:sqldatasource runat="server" CacheKeyDependency="MyTestKey"....
In code behind check Cache[CacheKeyDependency]. If it does not exists, create it:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//create CacheKeyDependency if it does not exists
if (Cache[SqlDataSource1.CacheKeyDependency] == null)
{
Cache[SqlDataSource1.CacheKeyDependency] = new object();
}
}
}
To refresh the cache use the following code:
Cache[SqlDataSource1.CacheKeyDependency] = new object();
You can have one CacheKeyDependency key word for multiple DataSource objects, so when you run
Cache["MyTestKey"] = new object();
Hope it helps.