如何设置的产出高速缓存指示》定义的控制,与没有代码在前面
-
09-06-2019 - |
题
我已经写了一个控制该继承的 System.Web.UI.WebControls.DropDownList
所以我没有任何代码前对于这一控制的,但我仍然想要设置OutputCache指令.我有以任何方式设置这C#代码,说有一个属性或类似的东西吗?
我特别希望能够复制的 VaryByParam
酒店
解决方案
我意识到这是一个令人难以置信的老问题,但它仍然是值得的答案。
你在说什么并不是一个用户控制它是一个自定义的控制。你想要做什么用的OutputCache可以做到的只是与背景下缓存。
在代码里你得到的数据和结合到你的下拉列表做这样的事情:
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();
其他提示
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);
不隶属于 StackOverflow