title: How to add a Templating to a UserControl?
body: <p>This question has been asked before</p>
  
  <ul>
  <li><a href="http://asp.net.bigresource.com/Web-Forms-Design-Time-Support-For-Custom-Templated-User-Control-in-VS-2008-CV9nSsXpF.html" rel="noreferrer">Web Forms :: Design Time Support For Custom Templated User Control In VS 2008</a></li>
  <li><a href="http://www.vbforums.com/showthread.php?t=580213" rel="noreferrer">VS 2008 Using the ITemplate in a UserControl and getting a design time rendoring error</a></li>
  <li><a href="https://stackoverflow.com/questions/10309086/how-to-show-the-controls-for-web-user-control-in-design-time">how to show the controls for web user control in design time?</a></li>
  <li><a href="http://forums.asp.net/t/1291600.aspx/1" rel="noreferrer">UserControl ITemplate Property Design Time Error - Easy for a Guru...</a></li>
  </ul>
  
  <p>but it doesn't hurt to ask it again:</p>
  
  <blockquote>
    <p>How do i add templating to a <code>UserControl</code> in ASP.net?</p>
  </blockquote>
  
  <h2>What hasn't worked so far</h2>
  
  <ol>
  <li><p>Start with a new <code>UserControl</code><sup>5</sup>, which i'll call <code>Contoso</code>:</p>
  
  <pre><code>public partial class Contoso: System.Web.UI.UserControl
  {
  }
  </code></pre>
  
  <p>This will allow us to use a new control:<sup>1</sup></p>
  
  <pre><code>&lt;Contoso&gt;
      Stuff in here
  &lt;Contoso&gt;
  </code></pre></li>
  <li><p>Create a public <code>ContentTemplate</code> property of type <code>ITemplate</code>:</p>
  
  <pre><code>public partial class Contoso: System.Web.UI.UserControl
  {
     public ITemplate ContentTemplate { get; set; }
  }
  </code></pre>
  
  <p>and add an indeterminate number of attributes to the <code>ContentTemplate</code> property:<sup>2</sup></p>
  
  <pre><code>//[ParseChildren(true)]
  [ParseChildren(true, "ContentTemplate")]
  //[ParseChildren(false)]
  public partial class Contoso: System.Web.UI.UserControl
  {
     [TemplateContainer(typeof(ContentContainer))]
     [TemplateInstance(TemplateInstance.Single)]
     [PersistenceMode(PersistenceMode.InnerProperty)]   
     //[PersistenceMode(PersistenceMode.InnerDefaultProperty)] 
     [Browsable(true)]
     //[Browsable(false)]
     [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
     //[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
     public ITemplate ContentTemplate { get; set; }
  }
  </code></pre>
  
  <p>this will allow us to add <code>&lt;ContentTemplate&gt;</code> to the control in our aspx file:<sup>1</sup></p>
  
  <pre><code>&lt;Contoso&gt;
     &lt;ContentTemplate&gt;
         Stuff in here
     &lt;/ContentTemplate&gt;
  &lt;/Contoso&gt;
  </code></pre></li>
  <li><p>Next we need to actually <em>use</em> the <code>ContentTemplate</code> stuff, by adding it somewhere. We do this by adding it to one of our UserControl's internal <code>div</code> elements. </p>
  
  <p>Starting from our <code>.aspx</code> file which was originally empty:</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contoso.aspx.cs" Inherits="Contoso" %&gt;
  </code></pre>
  
  <p>we add a parent <code>div</code> that will hold our <code>ContentTemplate</code> stuff:</p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contoso.aspx.cs" Inherits="Contoso" %&gt;
  &lt;div id="ContentDiv" runat="server"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Then we stuff the <code>ContentTemplate</code> stuff into that parent <code>div</code> during the control's <strong>Init</strong>:</p>
  
  <pre><code>public partial class Contoso: System.Web.UI.UserControl
  {
     protected override void OnInit(EventArgs e)
     {
        base.OnInit(e);
  
        //If there's content, then put it into our ContentDiv div
        if (this.ContentTemplate != null)
           this.ContentTemplate.InstantiateIn(ContentDiv);
     }
  
     [PersistenceModeAttribute(PersistenceMode.InnerProperty)]    
     [TemplateInstanceAttribute(TemplateInstance.Single)]
     [Browsable(true)]
     [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
     public ITemplate ContentTemplate { get; set; }
  }
  </code></pre></li>
  <li><p><strong>Edit</strong>: Indicate that your class implements <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx" rel="noreferrer"><code>INamingContainer</code></a>:</p>
  
  <pre><code>public partial class Contoso: System.Web.UI.UserControl: INamingContainer
  {
     protected override void OnInit(EventArgs e)
     {
        base.OnInit(e);
  
        //If there's content, then put it into our ContentDiv div
        if (this.ContentTemplate != null)
           this.ContentTemplate.InstantiateIn(ContentDiv);
     }
  
     [PersistenceModeAttribute(PersistenceMode.InnerProperty)]    
     [TemplateInstanceAttribute(TemplateInstance.Single)]
     [Browsable(true)]
     [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
     public ITemplate ContentTemplate { get; set; }
  }
  </code></pre>
  
  <p>The <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer.aspx" rel="noreferrer"><code>INamingContainer</code></a> interface does not have any members, and is only used to mark your <code>UserControl</code> class as something.</p></li>
  <li><p>And we're done<sup>3</sup>. We can now use this control in our aspx page. But first we need to <strong>"register"</strong> it at the top of our aspx page:</p>
  
  <pre><code>&lt;%@ Register src="Contoso.ascx" TagName="Contoso" tagprefix="uc" %&gt;
  </code></pre>
  
  <p>Where:</p>
  
  <ul>
  <li><code>Contoso.ascx</code> is the name of the <code>ascx</code> file</li>
  <li><code>Contoso</code> is the name of the element we will use to reference this user control</li>
  <li><code>uc</code> is a bit of text we will have to put in front of <code>uc:Contoso</code> (i use <code>uc</code> as short for <em>user-control</em>)</li>
  </ul></li>
  <li><p>Add the control to our page:</p>
  
  <pre><code>&lt;uc:Contoso ID="Crackers" runat="server"&gt;
      &lt;ContentTemplate&gt;
          Stuff goes here
      &lt;/ContentTemplate&gt;
  &lt;/qwerty:Contoso&gt;
  </code></pre></li>
  </ol>
  
  <p>And we're done!<sup>4</sup></p>
  
  <p><strong>Edit</strong>: Forgot to add the reason the above doesn't work. Visual Studio shows the error:</p>
  
  <blockquote>
    <p>Error Creating Control - Crackers</p>
    
    <p>Type 'System.Web.UI.UserControl' does not have a public property named 'ContentTemplate'</p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/F6n2x.png" alt="enter image description here"></p>
  
  <p>Which makes sense, since <code>UserControl</code> does not have a public property named <code>ContentTemplate</code> - so i can hardly blame it.</p>
  
  <h2>Series</h2>
  
  <p>This question is one in the ongoing Stackoverflow series, <strong>"Templating user controls"</strong>:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/11658053/how-to-add-a-templating-to-a-usercontrol/11700540">How to add a Templating to a UserControl?</a></li>
  <li><a href="https://stackoverflow.com/questions/12323303/how-to-inherit-from-control-rather-than-usercontrol">How to inherit from Control, rather than UserControl?</a></li>
  <li><a href="https://stackoverflow.com/questions/12321621/usercontrol-has-ispostback-but-control-does-not/12321692">UserControl has IsPostBack, but Control does not</a></li>
  <li><a href="https://stackoverflow.com/questions/11656637/usercontrol-does-not-have-public-property-named-contenttemplate">UserControl does not have public property named ContentTemplate</a></li>
  <li><a href="https://stackoverflow.com/questions/12325192/how-do-i-specify-codefilebaseclass-from-web-config">How do i specify CodeFileBaseClass from web.config?</a></li>
  </ul>
  
  <h2>Bonus Reading</h2>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/36574bf6.aspx" rel="noreferrer">How to: Create Templated ASP.NET User Controls</a></li>
  <li><a href="http://leedale.wordpress.com/2007/08/11/creating-a-templated-user-control-with-aspnet-20/" rel="noreferrer">Creating a Templated User Control with ASP.Net 2.0</a></li>
  <li><a href="http://geekswithblogs.net/CodeSpeaker/archive/2009/07/04/templated-user-controls-in-asp.net-for-better-maintainability.aspx" rel="noreferrer">Templated User Controls in ASP.NET for Better Maintainability</a></li>
  </ul>
  
  <h2>Footnotes</h2>
  
  <ul>
  <li><sup>1</sup> You can't ever use that syntax. That's just an easy to read and understand form.</li>
  <li><sup>2</sup> Nobody knows what attributes to add, or why. Add more or less attribute to taste.</li>
  <li><sup>3</sup> Not done. Done with the UserControl, but not our work.</li>
  <li><sup>4</sup> Not done; it doesn't work.</li>
  <li><sup>5</sup> in the web-site (not a web application, not in a separate assembly)</li>
  </ul>