asp.net.ph

Skip Navigation LinksASP.NET Applications > ASP.NET Optimization > ASP.NET Caching Features > Caching Portions of an ASP.NET Page > Caching Multiple Versions of User Control Output

Caching Multiple Versions of User Control Output

ASP.NET Web Applications   ASP .NET Caching Features   Caching Portions of an ASP.NET Page


Just as you can cache multiple versions of a page’s output, you can also cache any or all possible versions of a user control’s output on any given Web page.

There are some differences, though, between adding user control output to the cache and doing the same for the page. While both support multiple response caching based on HTTP GET and POST parameters, user controls do not support caching based on HTTP headers or custom strings.

Basically, ASP.NET allows caching multiple versions of user control output in either of two ways:

  • at design time, by declaratively using variable attributes on the @ OutputCache directive ( in the .ascx file ), or
  • at run time, by programmatically using the corresponding properties of the PartialCachingAttribute class ( when you develop the user control in a code-behind class ).

Specifically, the @ OutputCache directive includes two attributes that allow you to cache different versions of user control output, both based on GET query string or form POST parameters:

  • the VaryByParam attribute, and
  • the VaryByControl attribute.

NOTE: You can use either VaryByParam or VaryByControl depending on the need. You do not need to include both attributes in the directive.

Alternatively, the PartialCachingAttribute class provides two properties that allow you to do the same thing by adding an attribute to a user control in a code-behind class.

  • the PartialCachingAttribute.VaryByParams property
  • the PartialCachingAttribute.VaryByControls property

In addition, you can also cache multiple versions of a user control by declaring it more than once in its containing .aspx file. This is done by declaring text attributes when including the user control in the .aspx page, then using the attributes to customize how the user control processes the request.

More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note