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.
精彩评论