title: ASP.NET Programmatically enabling Output Caching does not work -> Why?
body: <p>why, in the below aspx and code behind, when Output Caching is enabled programmatically (enabled in code behind), it does not work and has a problem?  </p>
  
  <p><strong>aspx:</strong>  </p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" Inherits="ProgrammaticOutputCaching"
      CodeBehind="ProgrammaticOutputCaching.aspx.cs" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:Label ID="lblDate" runat="server" Font-Bold="False" Font-Names="Verdana" Font-Size="XX-Large"&gt;&lt;/asp:Label&gt;&lt;br /&gt;
          &lt;br /&gt;
          &lt;asp:Button ID="Button1" runat="server" Text="Refresh" /&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>code behind:</strong>   </p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          Response.Cache.SetCacheability(HttpCacheability.Public);
  
          // Use the cached copy of this page for the next 60 seconds.
          Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
          //Response.Cache.VaryByParams.IgnoreParams = true;
  
          // This additional line ensures that the browser can't
          // invalidate the page when the user clicks the Refresh button
          // (which some rogue browsers attempt to do).
          Response.Cache.SetValidUntilExpires(true);
  
          lblDate.Text = "The time is now:&lt;br&gt;" + DateTime.Now.ToString();
  }
  </code></pre>
  
  <p>with page directive for output caching there is no problem:<br>
  mean<br>
  <strong>aspx:</strong>   </p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" Inherits="OutputCaching" CodeBehind="OutputCaching.aspx.cs" %&gt;
  
  &lt;%@ OutputCache Duration="60" VaryByParam="Name;Age" Location="Server" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &amp;nbsp;&lt;asp:Label ID="lblDate" runat="server" Font-Bold="False" Font-Names="Verdana"
              Font-Size="XX-Large"&gt;&lt;/asp:Label&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;asp:Button ID="Button1" runat="server" Text="Refresh" /&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and<br>
  <strong>code behind:</strong>  </p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
          lblDate.Text = "The time is now:&lt;br&gt;";
          lblDate.Text += DateTime.Now.ToString();
  }
  </code></pre>
  
  <p>so what is the problem about programmatically?  </p>