<p>I've written a control that inherits from the <code>System.Web.UI.WebControls.DropDownList</code> and so I don't have any code in front for this control, but I still want to set the OutputCache directive.  I there any way to set this in the C# code, say with an attribute or something like that?  </p>

<p>I'm particularly hoping to be able to replicate the <code>VaryByParam</code> property</p>

[{'answer_id': 79012, 'author': 'dimarzionist', 'author_id': 10778, 'author_profile': 'https://Stackoverflow.com/users/10778', 'pm_score': 1, 'selected': False, 'text': '<pre><code>Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));\nResponse.Cache.SetCacheability(HttpCacheability.Server);\nResponse.Cache.SetValidUntilExpires(true);\n</code></pre>\n'}, {'answer_id': 3258564, 'author': 'matt-dot-net', 'author_id': 380518, 'author_profile': 'https://Stackoverflow.com/users/380518', 'pm_score': 3, 'selected': True, 'text': '<p>I realize this is an incredibly old question but it is still worthy of an answer.</p>\n\n<p>What you are talking about isn\'t a User Control it is a Custom Control.  What you want to do with the OutputCache can be done simply with the Context Cache.</p>\n\n<p>In your code where you are getting the data and binding to your DropDownList do something like this:</p>\n\n<pre><code>        List&lt;Object&gt; listOfObjects = null;\n//assuming a List of Objects... it doesn\'t matter whatever type of data you use\n        if (Context.Cache["MyDataCacheKey"] == null)\n        {\n            // data not cached, load it from database\n            listOfObjects = GetDataFromDB();\n//add your data to the context cache with a sliding expiration of 10 minutes.\n            Context.Cache.Add("MyDataCacheKey", listOfObjects, null,\n                System.Web.Caching.Cache.NoAbsoluteExpiration,\n                TimeSpan.FromMinutes(10.0),\n                System.Web.Caching.CacheItemPriority.Normal, null);\n        }\n        else\n            listOfObjects = (List&lt;Object&gt;)Context.Cache["MyDataCacheKey"];\n\n        DropDownList1.DataSource = listOfObjects;\n        DropDownList1.DataBind();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/78974', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2975/']