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.
7 comments:
Thanks for posting, I wasn't able to find any useful documentation on the CacheKeyDependency either. In my experience I've had to use Cache.Remove("MyTestKey") to reset the data source cache.
Cheers! Exactly what I was looking for!
Thanks, that helped me a lot!
Cheers
Vlad
Thanks my friend!
I was sending a plain string to the CachekKeyDependency but it wasn't doing the trick.
After reading your article I learned I was missing adding the same string to the application cache with the Cache class.
Thanks a lot man :)
Thanks for posting this. It was a realy help.
that seriously helped me out! thanks a lot mate
Thanks a lot, exactly what I was looking for!!!
Post a Comment