开发者

UserControl: How to set Output Cache duration programmatically?

开发者 https://www.devze.com 2022-12-23 22:15 出处:网络
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.

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

精彩评论

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