我编写了一个继承自System.Web.UI.WebControls.DropDownList
的控件,因此我没有任何代码用于此控件,但我仍想设置OutputCache指令。我有什么办法在C#代码中设置它,比如说属性或类似的东西?
我特别希望能够复制VaryByParam
属性
答案 0 :(得分:2)
我意识到这是一个令人难以置信的老问题,但仍然值得回答。
您所说的不是用户控件,它是自定义控件。您可以使用Context Cache简单地完成OutputCache的操作。
在您获取数据并绑定到DropDownList的代码中,执行以下操作:
List<Object> listOfObjects = null;
//assuming a List of Objects... it doesn't matter whatever type of data you use
if (Context.Cache["MyDataCacheKey"] == null)
{
// data not cached, load it from database
listOfObjects = GetDataFromDB();
//add your data to the context cache with a sliding expiration of 10 minutes.
Context.Cache.Add("MyDataCacheKey", listOfObjects, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(10.0),
System.Web.Caching.CacheItemPriority.Normal, null);
}
else
listOfObjects = (List<Object>)Context.Cache["MyDataCacheKey"];
DropDownList1.DataSource = listOfObjects;
DropDownList1.DataBind();
答案 1 :(得分:1)
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);