Using PartialCaching with dynamically added Webcontrols

  • Thread starter Thread starter DC
  • Start date Start date
D

DC

Hi,

we are dynamically adding webcontrols in a page like so:

Type t = Type.GetType("controls.namespace.control, controls.namespace",
false, true);
Control c = (Control)Activator.CreateInstance(t, true);
this.Controls.Add(c);

Now we would like to cache this webcontrol.

Using the same approach with user controls makes caching easier since
..LoadControl will return a PartialCachingControl which is "ready for
caching". Is there a similar approach for webcontrols?

When I simply cache control c in the example above I can reuse it, but
when multiple threads are adding such cached controls to their control
tree then the CreateChildControls process will throw exceptions (since
the same instance of one controls is being used simultaneously in
multiply control trees - I guess).

TIA for any idea!

Regards
DC
 
No clue, anyone? We can use some complicated stuff like below, but
using reflection seems to drag the performance down unnecessarily.

Here are two excellent articles on the topic:
http://weblogs.asp.net/dmarsh/archive/2003/11/25/39793.aspx
http://weblogs.asp.net/dmarsh/archive/2003/11/26/39957.aspx

I was hoping somebody here knows a snappier design.

public Control LoadWebControl(string typeName, string controlID)
{
Type controlType = Type.GetType(typeName, false, true);

PartialCachingAttribute cacheAttribute = new
PartialCachingAttribute(120, null, null, null, true);

PartialCachingControl cachedControl =
(PartialCachingControl)Activator.CreateInstance(
typeof(PartialCachingControl),
BindingFlags.NonPublic
| BindingFlags.Public
| BindingFlags.ExactBinding
| BindingFlags.Instance,
null,
new object[] {
controlType,
cacheAttribute,
controlID
},
CultureInfo.InvariantCulture
);
return cachedControl;
}
 
Back
Top