HOWTO do the wrong thing: Declaratively cache a page by the value of a usercontrol's child control

Scenario: You have an ASP.NET 1.x page that contains a user control with a drop-down list [1] in it. You want to declaratively output-cache the whole page based on the value selected in that drop-down list [1], but not by anything else (yes, this is very-much a niche scenario).

Howto (bad): Use the @ OutputCache directive and specify the name of the drop-down list (as it appears in the generated HTML - the name that will form part of the HTTP POST) as part of the varybyparam attribute. This will typically be something like userControlName:dropDownListName.

 <%@ OutputCache Duration=“100” VaryByParam=“userControlName:dropDownListName” %>

Why this might be a bad idea: This kind of breaks encapsulation, since you’re relying on the name of the child control (which may not be publicly exposed in code) to control the cachability of your page. If either the name of the user control or the contained control change, of the usercontrol moves to a different naming container then caching will silently stop working on this page. This is not that different to the way the outputcache directive works normally - it is not in any way “strongly typed“. You can put anything in that varybyparam attribute and if it isn’t there it will just get ignored.

[1] - replace ‘drop-down list’ with whatever type of control you’re interested in here.