How to add a Templating to a UserControl?

Viewed 9185

This question has been asked before

but it doesn't hurt to ask it again:

How do i add templating to a UserControl in ASP.net?

What hasn't worked so far

  1. Start with a new UserControl5, which i'll call Contoso:

    public partial class Contoso: System.Web.UI.UserControl
    {
    }
    

    This will allow us to use a new control:1

    <Contoso>
        Stuff in here
    <Contoso>
    
  2. Create a public ContentTemplate property of type ITemplate:

    public partial class Contoso: System.Web.UI.UserControl
    {
       public ITemplate ContentTemplate { get; set; }
    }
    

    and add an indeterminate number of attributes to the ContentTemplate property:2

    //[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; }
    }
    

    this will allow us to add <ContentTemplate> to the control in our aspx file:1

    <Contoso>
       <ContentTemplate>
           Stuff in here
       </ContentTemplate>
    </Contoso>
    
  3. Next we need to actually use the ContentTemplate stuff, by adding it somewhere. We do this by adding it to one of our UserControl's internal div elements.

    Starting from our .aspx file which was originally empty:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contoso.aspx.cs" Inherits="Contoso" %>
    

    we add a parent div that will hold our ContentTemplate stuff:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contoso.aspx.cs" Inherits="Contoso" %>
    <div id="ContentDiv" runat="server"></div>
    

    Then we stuff the ContentTemplate stuff into that parent div during the control's Init:

    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; }
    }
    
  4. Edit: Indicate that your class implements INamingContainer:

    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; }
    }
    

    The INamingContainer interface does not have any members, and is only used to mark your UserControl class as something.

  5. And we're done3. We can now use this control in our aspx page. But first we need to "register" it at the top of our aspx page:

    <%@ Register src="Contoso.ascx" TagName="Contoso" tagprefix="uc" %>
    

    Where:

    • Contoso.ascx is the name of the ascx file
    • Contoso is the name of the element we will use to reference this user control
    • uc is a bit of text we will have to put in front of uc:Contoso (i use uc as short for user-control)
  6. Add the control to our page:

    <uc:Contoso ID="Crackers" runat="server">
        <ContentTemplate>
            Stuff goes here
        </ContentTemplate>
    </qwerty:Contoso>
    

And we're done!4

Edit: Forgot to add the reason the above doesn't work. Visual Studio shows the error:

Error Creating Control - Crackers

Type 'System.Web.UI.UserControl' does not have a public property named 'ContentTemplate'

enter image description here

Which makes sense, since UserControl does not have a public property named ContentTemplate - so i can hardly blame it.

Series

This question is one in the ongoing Stackoverflow series, "Templating user controls":

Bonus Reading

Footnotes

  • 1 You can't ever use that syntax. That's just an easy to read and understand form.
  • 2 Nobody knows what attributes to add, or why. Add more or less attribute to taste.
  • 3 Not done. Done with the UserControl, but not our work.
  • 4 Not done; it doesn't work.
  • 5 in the web-site (not a web application, not in a separate assembly)
1 Answers
Related