![]() |
Web Development »
ASP.NET Controls »
General
Intermediate
License: The Code Project Open License (CPOL)
Hack to enforce the cache of an XmlDataSource to invalidateBy percyboyI found that when you change the Data property of an XmlDataSource control via code, it will not automatically invalidate the cache. This is a hack for this problem. |
C# (C# 1.0, C# 2.0, C# 3.0), Windows, .NET (.NET 2.0, .NET 3.0, .NET 3.5), ASP.NET, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
We often use the XmlDataSource control together with the TreeView control.
If the data source is static, it works quite well. But, when the data source needs to be dynamic, there seems to be some problems. This article will introduce one such problem and give you a hack solution.
If the data source needs to be dynamic, you may choose to:
XmlDataSource controls on the web form, and switch it by setting different DataSourceIDs. XmlDataSource control and change the value of the DataFile or Data property of it, when necessary. Obviously, the first way is more limited and only applicable for some special situations. It's not a good idea to put a lot of data source controls on one form and, more importantly, the count of the data source controls must be a fixed number.
But, what will happen when you change the value of the DataFile or Data property of the XmlDataSource control? From Microsoft's documentations, you may find the following information:
If you change the value of the
DataFile/Dataproperty, theDataSourceChangedevent is raised. If caching is enabled and you change the value ofDataFile/Data, the cache is invalidated.
Unfortunately, in my test (you may download the samples to see it yourself), I found that, the cache will not be automatically invalidated when you change the value of the Data property of the XmlDataSource control, although DataFile makes no trouble.
First, you may choose to change the value of the DataFile property of the XmlDataSource, instead of Data. This is an easy solution.
Otherwise, if you stick to changing the property of Data for some reason (for example, your data source is not a real XML file, but just a dynamically-populated string), you may try the following hack method:
public static void XmlDataSourceCacheHack(XmlDataSource dataSource)
{
try
{
Type t = typeof(XmlDataSource);
MethodInfo m = t.GetMethod("CreateCacheKey",
BindingFlags.Instance | BindingFlags.NonPublic);
string key = (string)m.Invoke(dataSource, null);
PropertyInfo p = t.GetProperty("Cache",
BindingFlags.Instance | BindingFlags.NonPublic);
object cache = p.GetValue(dataSource, null);
Type t2 = t.Assembly.GetType("System.Web.UI.DataSourceCache");
MethodInfo m2 = t2.GetMethod("Invalidate",
BindingFlags.Instance | BindingFlags.Public);
m2.Invoke(cache, new object[] { key });
}
catch
{
}
}
(It's sure that "using System.Reflection;" is required at the beginning of the file.)
This is just a hack for this problem. I spent more than one day on debugging to find the source of this strange problem.
(P.S.: If you find a more perfect way to solve it, avoiding Reflection, do tell me. Thanks!)
You may surely entirely disable the cache feature of the XmlDataSource. It also works fine. This is another easy solution.
But, if your situation is just like mine: the change possibilities are lower than other postback possibilities (for example, the TreeView is quite big and always triggers postbacks more frequently than data sources change), you will prefer using Reflection than disabling the cache.
I'm not sure whether this is a bug from Microsoft, or if it is just applied to the design specification. But, either the implementation is wrong, or the documentation is wrong.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 16 Jun 2008 Editor: Smitha Vijayan |
Copyright 2008 by percyboy Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |