开发者

UserControl: How to set Output Cache duration programmatically?

UserControl: How to set Outpu开发者_StackOverflow社区t Cache duration programmatically?


Make a public property on the UserControl which you could use to change the duration of the cache.

 public Double CacheDuration { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.Server);
            Response.Cache.SetExpires(DateTime.Now + TimeSpan.FromSeconds(CacheDuration));
            Response.Write(DateTime.Now.ToString());
        }

Then when you add the UserControl, mine is named Cached, you could do this.

 var cachedUserControl = new Cached {CacheDuration = 5};
            Page.Controls.Add(cachedUserControl);


You can sue Response.Cache.SetExpires(DateTime DateTime); for that. Some useful infor here as well How to enable ASP.NET output caching programmatically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜