This question has been asked before
- Web Forms :: Design Time Support For Custom Templated User Control In VS 2008
- VS 2008 Using the ITemplate in a UserControl and getting a design time rendoring error
- how to show the controls for web user control in design time?
- UserControl ITemplate Property Design Time Error - Easy for a Guru...
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
Start with a new
UserControl
5, which i'll callContoso
:public partial class Contoso: System.Web.UI.UserControl { }
This will allow us to use a new control:1
<Contoso> Stuff in here <Contoso>
Create a public
ContentTemplate
property of typeITemplate
: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>
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 internaldiv
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 ourContentTemplate
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 parentdiv
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; } }
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 yourUserControl
class as something.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 theascx
fileContoso
is the name of the element we will use to reference this user controluc
is a bit of text we will have to put in front ofuc:Contoso
(i useuc
as short for user-control)
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'
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":
- How to add a Templating to a UserControl?
- How to inherit from Control, rather than UserControl?
- UserControl has IsPostBack, but Control does not
- UserControl does not have public property named ContentTemplate
- How do i specify CodeFileBaseClass from web.config?
Bonus Reading
- How to: Create Templated ASP.NET User Controls
- Creating a Templated User Control with ASP.Net 2.0
- Templated User Controls in ASP.NET for Better Maintainability
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)