Use a cache substitution widget

To substitute the cache for a particular widget, you create a custom cache substitution widget and place it on the page. The widget utilizes the ASP.NET cache substitution. You create a static method that registers with the ASP.NET implementation of cache substitution. Inside this method, you must render the content of the widget.

Create the widget markup

Create a User widget with the following markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CacheSubstitutionWidget.ascx.cs" Inherits="SitefinityWebApp.CacheSubstitutionControl" %>
<asp:Button runat="server" ID="refreshBtn" Text="Refresh Time" />

Implement the code behind

If the widget, you want to substitute, inherits from SimpleView, which in turn inherits from System.Web.UI.WebControls.WebControl, you can override the RenderContents(System.Web.UI.HtmlTextWriter writer) method. Inside, you call your own implementation for performing cache substitution.

When output cache is used on the page, you substitute it with the CacheSubstitutionWrapper in DoPostCacheSubstitution() method by forming a dictionary of parameters and passing it to a CacheSubstitutionWrapper.RenderMarkupDelegate that afterwards renders the correct markup.

IMPORTANT: Do not pass entire objects, such as a page, a control instance, property of the control, etc. This will cause serious memory leaks. When you need to pass a property value, you must use its string representation.

Following is a code sample of the code-behind file:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web.UI.PublicControls;
namespace SitefinityWebApp
{
public partial class CacheSubstitutionControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void RenderControl(HtmlTextWriter writer)
{
if (!SystemManager.IsDesignMode)
{
DoPostCacheSubstitution();
}
else
base.RenderControl(writer);
}
/// <summary>
/// When OutputCache is used on the page, we substitute it with the CacheSubstitutionWrapper
/// </summary>
private void DoPostCacheSubstitution()
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
//you can pass parameters to the method which will render the cache substitution markup
parameters.Add("dateTimeFormat", @"yyyy'-'MM'-'dd'T'HH':'mm':'ss");
CacheSubstitutionWrapper cacheSubstitutionWrapper = new CacheSubstitutionWrapper(parameters, new CacheSubstitutionWrapper.RenderMarkupDelegate(CacheSubstitutionControl.RenderCacheSubstitutionMarkup));
cacheSubstitutionWrapper.RegisterPostCacheCallBack(this.Context);
}
/// <summary>
/// This method is used by the CacheSubstitutionWrapper delegate and is used when the page
/// uses OutputCache to render the correct markup
/// </summary>
internal static string RenderCacheSubstitutionMarkup(Dictionary<string, string> parameters)
{
StringBuilder resultMarkup = new StringBuilder();
resultMarkup.AppendFormat("Current server time is {0}", DateTime.Now.ToString(parameters["dateTimeFormat"]));
resultMarkup.AppendFormat("<input type=\"submit\" value=\"Refresh Time\" name=\"ctl17$C$ctl00$ctl03$C$C004$refreshBtn\">");
return resultMarkup.ToString();
}
}
}

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?