Using LoadControl to display cached WebUserControls

671 Views Asked by At

This is my first post here.

I've written a CMS which allows developers to write their own plugins in the form of WebUserControls. These plugins can be inserted (via TinyMCE) into the content by the users. This is done programatically using the LoadControl method, adding to the controls hierarchy and setting it's properties. All good so far.

I want to allow developers to use OutputCache on their WebUserControls, however these controls need to be cached by the properties of the WebUserControl. e.g. a developer has written a WebUserControl color.ascx which has a property Color (Red|Green|Blue) so depending on the value of this property the cache could store a Red, Green and a Blue version.

The problem that I have is that a page can have more than one, let's say three, color.ascx controls on it (Red, Green and Blue) dynamically loaded with the LoadControl. After being cached the LoadControl method correctly returns a PartialCachingControl but when refreshing page it is always the last version from the cache, i.e. the page shows 3 Blue instead of Red, Green and Blue.

Is there a way with LoadControl (or by some other means) to load the correct version from the cache, i.e. using the Color property, and how should I use OutputCache to ensure it is saved to the cache based on a WebUserControls properties?

Thanks for your help.

2

There are 2 best solutions below

2
Alex D' On

It seems, you've got a classical LoadControl caching problem. Called from the same place (for-each loop for example) in your program, for the same control this function always returns the same instance, if cache was required. To avoid this, in your case, just make 3 different calls - 3 different lines of code in your application

0
tommycode On

I managed to come around this problem using a DynamicMethod. Full description and code can be found in the blog entry I wrote after I solved it:

http://www.tommycode.se/2012/04/outputcache-on-dynamically-loaded.html