NewsRotator control uses the WriteSubstitution() when displaying a random news item. : Post Cache « Cache « ASP.NET Tutorial






If you use this control in a page that has been output cached, the NewsRotator control continues to display news items randomly.

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
namespace myControls
{
    public class NewsRotator : WebControl
    {

        public static string GetNews(HttpContext context)
        {
            List<String> news = new List<string>();
            news.Add("Martians attack!");
            news.Add("Moon collides with earth!");
            news.Add("Life on Jupiter!");

            Random rnd = new Random();
            return news[rnd.Next(news.Count)];
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            Context.Response.WriteSubstitution(GetNews);
        }

    }
}








13.10.Post Cache
13.10.1.Using Post-Cache Substitution
13.10.2.Use post-cache substitution programmatically by using the Response.WriteSubstitution() method.
13.10.3.NewsRotator control uses the WriteSubstitution() when displaying a random news item.