IMPORTANT: This version of Sitefinity CMS is out of support and the respective product documentation is no longer maintained and can be outdated. Use the version selector to view a supported product version.
Use this tutorial to avoid caching on a PostBack by overriding the default caching rules.
EXAMPLE: You have a menu that is created from custom HTML code and output as a string, based on the page structure. If the page is cached, change in the page structure will not be immediately visible in the menu. The page structure does not change often, therefore, rebuilding the menu should be executed only when the page structure changes, not on every page request.
You do this by providing a different key for each page, using each page URL and a static widget GUID to generate a unique hash key.
When the page structure does change, you need to go through all the pages, create a hash and then remove any keys from the cache. You need to implement code that handles this event and then subscribe to the page Publish event. The next time the page loads, the menu is rebuilt according to the new page structure.
Publish
When the page runs the cache substitution, it checks whether the HTML already exists in the cache. If it does, it means that the page structure has not changes, so it is served from the cache. When a page is loaded from the cache, the only method that is executed on page load is the RenderCacheSubstitutionMarkup that passes the dictionary of parameters as strings.
RenderCacheSubstitutionMarkup
To implement cache substitution, perform the following:
Init
public String PageUrl { get; set; }
protected override void InitializeControls(GenericContainer container)
{
if (!SystemManager.IsDesignMode)
PageSiteNode page = SiteMapBase.GetActualCurrentNode();
if (page != null)
PageUrl = page.Url;
}
private static readonly Guid menuId = new Guid("a3b20fe5-14af-466d-9c23-90ba89c26cb9");
private static readonly ICacheManager cache = SystemManager.GetCacheManager(CacheManagerInstance.Global);
protected override ControlSimpleView.CacheLevel ControlCacheLevel
get { return CacheLevel.ContentListener; }
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
if (this.ControlCacheLevel != CacheLevel.AlwaysCache)
Dictionary<
String
, String> parameters = new Dictionary<
, String>();
parameters.Add("PageUrl", PageUrl);
CacheSubstitutionWrapper cacheSubstitutionWrapper = new CacheSubstitutionWrapper(parameters, new CacheSubstitutionWrapper.RenderMarkupDelegate(Menu.RenderCacheSubstitutionMarkup));
cacheSubstitutionWrapper.RegisterPostCacheCallBack(this.Context);
internal static String RenderCacheSubstitutionMarkup(Dictionary<
, String> parameters)
String menuHtml = String.Empty;
String pageUrl = parameters["PageUrl"];
var hashKey = Common.CalculateMD5Hash(pageUrl + menuId.ToString());
if(cache.Contains(hashKey))
menuHtml = (String)cache.GetData(hashKey);
else
menuHtml = BuildMenu(pageUrl);
cache.Add(hashKey, menuHtml);
return menuHtml;
public static void PageManagerExecuting(Object sender, Telerik.Sitefinity.Data.ExecutingEventArgs e)
if (e.CommandName == "CommitTransaction")
// Removed content revelant to finding if this page is being published
// Find all pages and clear any menu html
var siteMapProvider = SiteMapBase.GetSiteMapProvider("FrontendSiteMap");
PageSiteNode nodeToTraverse = (PageSiteNode)siteMapProvider.RootNode;
if (nodeToTraverse.ChildNodes.Count > 0)
// We are only looking for the top level pages so no need to traverse child pages.
foreach (var childNode in nodeToTraverse.ChildNodes)
var node = (PageSiteNode)childNode;
var hashKey = Common.CalculateMD5Hash(node.Url + menuId.ToString());
cache.Remove(hashKey);
if (e.CommandName == "Bootstrapped")
PageManager.Executing += new EventHandler<
Telerik.Sitefinity.Data.ExecutingEventArgs
>(Menu.PageManagerExecuting);
Back To Top
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important