Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well I believe you almost got it.</p> <p>BTW. The UserControl is not rendered using Visual Studio Designer, however when you run the application the control works. This is different if you use Server Controls instead, in that case, the control is displayed correctly in the Visual Studio designer</p> <p>The following code works great to build <strong>templated user controls and templated server controls</strong> however, if you would like to add binding capabilities, the process is slightly different, <a href="http://msdn.microsoft.com/en-us/library/aa479322.aspx" rel="noreferrer">take a look</a></p> <p><a href="http://sdrv.ms/N3hTFA" rel="noreferrer">Download Source Code</a></p> <h2>This is the code to create a templated <code>UserControl</code>.</h2> <h3>Simple Output</h3> <p><img src="https://i.stack.imgur.com/5sPXi.png" alt="enter image description here"></p> <h3>Template Container</h3> <pre><code>public class MyTemplateContainer : Control, INamingContainer { } </code></pre> <h3>ASPX Code behind</h3> <pre><code>protected void Page_Load(object sender, EventArgs e) { // just to demonstrate using the contorl this.WebUserControl1.Controls.Add(new LiteralControl("&lt;br /&gt;new control")); } </code></pre> <h3>ASPX</h3> <pre><code>&lt;%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %&gt; &lt;uc1:WebUserControl ID="WebUserControl1" runat="server"&gt; &lt;ContentTemplate&gt; My Template&lt;br /&gt; &lt;asp:Label Text='Hello People' runat="server" ID="lblMessage" /&gt; &lt;/ContentTemplate&gt; &lt;/uc1:WebUserControl&gt; </code></pre> <h3>ASCX Code behind</h3> <pre><code>public partial class WebUserControl : System.Web.UI.UserControl { [TemplateContainer(typeof(MyTemplateContainer))] [TemplateInstance(TemplateInstance.Single)] [PersistenceMode(PersistenceMode.InnerProperty)] [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public ITemplate ContentTemplate { get; set; } protected void Page_Init(object sender, EventArgs e) { this.myPlaceHolderTag.Controls.Clear(); if (this.ContentTemplate != null) { var container = new MyTemplateContainer(); this.ContentTemplate.InstantiateIn(container); this.myPlaceHolderTag.Controls.Add(container); } else { this.myPlaceHolderTag.Controls.Add(new LiteralControl("No template defined")); } } } </code></pre> <h3>ASCX</h3> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %&gt; &lt;asp:PlaceHolder runat="server" ID="myPlaceHolderTag" /&gt; </code></pre> <h2>Code to add a templated Server Control</h2> <h3>Output</h3> <p><img src="https://i.stack.imgur.com/VI9Kb.png" alt="enter image description here"></p> <h3>ASPX</h3> <pre><code>&lt;%@ Register Namespace="MyControls" TagPrefix="my" %&gt; &lt;my:MyServerControl runat="server" ID="myServerControl"&gt; &lt;ContentTemplate&gt; My Server templated control&lt;br /&gt; &lt;asp:Label Text="My Label" runat="server" /&gt; &lt;/ContentTemplate&gt; &lt;/my:MyServerControl&gt; </code></pre> <h3>Template Container</h3> <pre><code>namespace MyControls { [ToolboxItem(false)] public class MyTemplateContainer : Control, INamingContainer { } } </code></pre> <h3>Templated Server Control</h3> <pre><code>namespace MyControls { [ToolboxData("&lt;{0}:MyServerControl runat=server &gt;")] [ToolboxItem(true)] [ParseChildren(true)] // you can inherit from another control if you like, for example from the CompositeControl public class MyServerControl : Control, INamingContainer { [TemplateInstance(TemplateInstance.Multiple)] [TemplateContainer(typeof(MyTemplateContainer))] [PersistenceMode(PersistenceMode.InnerProperty)] [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [DefaultValue(null)] public ITemplate ContentTemplate { get; set; } protected override void CreateChildControls() { var p = new Panel { ID = "myPanel", BackColor = Color.Silver, Width = new Unit("100%") }; if (this.ContentTemplate == null) { p.Controls.Add(new LiteralControl("No content has been specified")); } else { var c = new MyTemplateContainer(); this.ContentTemplate.InstantiateIn(c); p.Controls.Add(c); } this.Controls.Clear(); this.Controls.Add(p); } public override void DataBind() { this.CreateChildControls(); this.ChildControlsCreated = true; base.DataBind(); } public override ControlCollection Controls { get { this.EnsureChildControls(); return base.Controls; } } } } </code></pre> <p>References:</p> <ul> <li><p><a href="http://msdn.microsoft.com/en-us/library/36574bf6.aspx" rel="noreferrer">How to: Create Templated ASP.NET User Controls</a></p></li> <li><p><a href="http://msdn.microsoft.com/en-us/library/aa478964.aspx" rel="noreferrer">Building Templated Custom ASP.NET Server Controls</a></p></li> <li><p><a href="http://msdn.microsoft.com/en-us/library/aa479322.aspx" rel="noreferrer">Building DataBound Templated Custom ASP.NET Server Controls</a></p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload