Home » Advanced CSharp » ASP.NET Substitution Control

ASP.NET Substitution Control


Tucked away on the toolbar is a little-used and often overlooked control.  Not using this control could be costing you in performance.

The control I’m referring to is the Substitution control.  The only time you’d use it would be if you had implemented page caching.  You are using page caching, right?

What?  You aren’t because you have a section of each page that needs to be updated, otherwise you would?

That is exactly what the substitution control was made for.

Create an ASPX page and implement caching at the top

<%@ OutputCache Duration="60" VaryByParam="None"%>

Now put a label on the page

<asp:label id="m_labelDate" runat="server"

in the page_load event handler, set the date and time into the label

protected void Page_Load(object sender, EventArgs e)
    m_labelDate.Text = DateTime.Now.ToLongTimeString();

If you run this now, you’ll see that no matter  how many times you refresh the page, the time will stay the same for a minute.  Once the minute is up, the time will change.  This is because the page is being retrieved from the cache each time.

To implement caching and get the date to update, you’ll want to use the Substitution control.  We’ll need to implement a static method in the page class for this to work first.

public static string CurrentTimeString(HttpContext context)
    return DateTime.Now.ToLongTimeString();


e didn’t use it here, but notice that we are passing in the HttpContext which will allow us to access items like the session object.

Now place the substitution control on the page and tell it to call the CurrentTimeString method we created above.

<asp:Substitution ID="Substitution1" 
runat="server" MethodName="CurrentTimeString" />

Now every time you refresh the page, you’ll get the new time, but all that will get called of the page code is the static method.  You can see this best by leaving the label in when you put in the substitution control.  The label will stay the same and the substitution control will change.


Other post in Advanced CSharp

Related Post

  • DotNetNuke Modules – CachingDotNetNuke Modules – Caching A couple of days ago I showed a method for finding a module on another page. Néstor Sánchez pointed out that on a large site, this could cause a significant performance issue. He […]
  • ASP.NET Assigning a Role to a UserASP.NET Assigning a Role to a User Another function that is not supplied by one of the existing controls in ASP.NET is the ability to assign a user to a role.  For this, we will need to resort to using the APIs. […]
  • Determine The Role of a User in ASP.NETDetermine The Role of a User in ASP.NET There are several controls that allow you to display content based on the role a user is in, including: - LoginView - LoginStatus And the web.config file allows us to control which […]
  • ASP.NET Three Tiered w/ Client Side DataASP.NET Three Tiered w/ Client Side Data Last week, I created a tool that would allow the user to upload an XML file and have the web site process the file and return a report. All pretty standard stuff until you realize that if […]
  • Tracking Down Performance Issues in ASP.NETTracking Down Performance Issues in ASP.NET Last night, one of my clients assigned me a problem that I thought was going to require one solution, and in the end it was just poor programming. But the process reminded me of the need […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.