繁体   English   中英

如何在没有代码的情况下在自定义控件上设置输出缓存指令

[英]How to set the output cache directive on custom controls with no code in front

我已经编写了一个继承自System.Web.UI.WebControls.DropDownList的控件,因此该控件的前面没有任何代码,但是我仍然想设置OutputCache指令。 我有什么方法可以在C#代码中进行设置,例如使用属性还是类似的名称?

我特别希望能够复制VaryByParam属性

我意识到这是一个令人难以置信的古老问题,但仍然值得回答。

您所说的不是用户控件,而是自定义控件。 您想要使用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();
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM