Jitesh Byahut 9/28/2014 2632

Partial Page Cache

Sometimes you want to cache a portion of page instead of caching entire page. So in this condition you can use partial page caching that cache user control. So you can cache a specific portion of page that not frequently changes.

Caching with a User Control

If you want to cache only small part of page instead of entire page then you should use advantage of User Controls. User control cache is exactly same as Asp.Net page caching. Same as page you have to add OutPutCache directive in User Control. One thing is notice that User control always cached the content on web server not on proxy server or browsers because proxy servers or browses always cached entire page.

<%@ Control Language="C#" CodeFile="Asp.ascx.cs" Inherits="Asp" %>

<%@ OutputCache Duration="43200" VaryByParam="none" %>

Sharing a User Control Output Cache

If one User Control located on different pages then by default it’s not share the same cache. If you want to cache the same User Control content on multiple pages then you have to specify Shared attribute in OutputCache directive in User Control.

<%@ Control Language="C#" CodeFile="CSharp.ascx.cs" Inherits="UserControl_CSharp" %>

<%@ OutputCache Duration="43200" VaryByParam="none" Shared="true" %>

User Control Cache by Programmatically

You can load user control by programmatically.

CachePolicy.Duration = TimeSpan.FromMinutes(10);

CachePolicy.SetSlidingExpiration(true);

CachePolicy.VaryByParams["none"] = true;

Caching dynamically loaded User Control

You can load user control dynamically by programmatically at run time.

protected void Page_Load(object sender, EventArgs e)

{

     PartialCachingControl partialCache=(PartialCachingControl)Page.LoadControl("../UserControl/CSharp.ascx");

 

     partialCache.CachePolicy.Duration = TimeSpan.FromMinutes(10);

     partialCache.CachePolicy.SetExpires(DateTime.Now.AddMinutes(10));

 

     Page.Controls.Add(partialCache);

}


Please give your feedback for improving this page