views:

679

answers:

5

I have created a user control in my application "header.ascx", I am pasing a selectedMenu attribute to this control on which the control selects the selectedMenu value specified. Suppose, I have passed value "home" or "search" then it will select (highlight it) the search menu.

I want to cache this control, When the value of the selectedMenu attribute changes then only the cache will be refreshed else it should picks up the control from cache??

Is it possible to cache a user control in asp.net?? I am using ASP.NET 2.0 (C#)

A: 

This may help Caching?

Dean
Is that article relevant to ASP.NET 2.0 as well ?
Cerebrus
+2  A: 

Of course you can! It's called "Fragment Caching". Here's a link to the Quickstarts page and the MS Knowledge base. Additionally, Google.

Cerebrus
A: 

User control caching in ASP.NET is called fragment caching. It's done by adding an OutputCache directive to the top of your page: <%@ OutputCache Duration="120" VaryByParam="None" %>

You can't vary the cache by setting the property on the control because the control isn't actually created if it's found in the cache. If you try to access the control in the code behind it's cached, it will be null.

Is the condition that determines whether the control should be cached or not something that you can determine by looking at the current request? If it is, you can use the varybycustom attribute (http://msdn.microsoft.com/en-us/library/system.web.ui.partialcachingattribute.varybycustom.aspx) of the output cache directive. You can put any string you want in there as the parameter and then when the caching is evaluated the GetVaryByCustomString() method from Global.asxa will be called and you can put the logic for whether the control should be cached or not there.

Helephant
@Helephant, In this case I suspect they could probably use VaryByControl.
LukeH
Yes, it would be VaryByControl.
Cerebrus
+1  A: 

I don't think it's a good idea to cache the control itself:

  • When the control is created the very first time, it has references to its parent page among others.
  • When you retrieve the control from the cache, those references does no longer exists.

A better approach, I think, is to cache the data which the control is using instead. ASP.NET creates so many controls during the page life cycle, that caching this one control really doesn't improve anything.

Then a stupid question at the end: Is this control a bottleneck? Do you really need the cache?

Thomas Eyde
A: 

To summarize

using VaryByCustom, means

1- Build the control again.
2- Having multiple versions of the control in the cache. which will be used when the custom conditions meet.

This is only good if the control is taking a lot of time to build and we have finite number of cached versions to not waste memory, and the application will need to access control properties (while it is cached "or null").

but it will not be good if that custom conditions are depending on the control properties itself. I can't access it, it is null.

for example I want to write something like if (the default selected value in countries list is NOT USA) then rebuild and Cache (give it a different string)

Otherwise don't

while other objects are trying to access the contries list, it will check for null, and set the countries drop down list to USA.

The data cahing will do the work. it is the only way.

who agree?

Thanks for ur time

Costa