Using PartialCaching with dynamically added Webcontrols

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
 
D

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top