Programming Microsoft ASP.NET 4 - Dino Esposito [403]
If you plan to cache user controls—that is, if you’re trying for partial caching—it’s probably because you just don’t want to, or cannot, cache the entire page. However, a good question to ask is this: What happens if user controls are cached within a cacheable page?
Both the page and the controls are cached individually, meaning that both the page’s raw response and the control’s raw responses are cached. However, if the cache duration is different, the page duration wins and user controls are refreshed only when the page is refreshed.
A cacheable user control can be embedded both in a cacheable page and in a wrapper-cacheable user control.
Important
Cacheable user controls should be handled with extreme care in the page’s code. Unlike regular controls, a user control marked with the @OutputCache directive is not guaranteed to be there when your code tries to access it. If the user control is retrieved from the cache, the property that references it in the code-behind page class is just null.
if (CustomerGrid1 != null)
CustomerGrid1.Country = "USA";
To avoid bad surprises, you should always check the control reference against the null value before executing any code.
Advanced Caching Features
The output caching subsystem has also a few other cool features to offer. They are caching profiles and post-cache substitution. In brief, caching profiles let you save a block of output caching-related settings to the configuration file. Post-cache substitution completes the ASP.NET offering as far as output caching is concerned. In addition to saving the entire page or only fragments of the page, you can now also cache the entire page except for a few regions.
Caching Profiles
varyByCustom="..." varyByControl="..." varyByHeader="..." varyByParam="..." noStore=true|false" /> Basically, by defining a named entry in the <%@ OutputCache CacheProfile="MySettings" %> In the preceding code, the application has a MySettings entry in the Post-Cache Substitution To use post-cache substitution, you place a new control—the The MethodName property must be set to the name of a static method that can be encapsulated in an HttpResponseSubstitutionCallback delegate, as follows: public static string WriteTimeStamp(HttpContext
The @OutputCache directive for pages supports the CacheProfile string attribute, which references an entry under the
With user controls, you can cache only certain portions of ASP.NET pages. With post-cache substitution, you can cache the whole page except specific regions. For example, using this mechanism, an AdRotator control can serve a different advertisement on each request even if the host page is cached.