How to add a Templating to a UserControl?

血红的双手。 提交于 2019-11-27 04:23:58

问题


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

  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'

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)

回答1:


Well I believe you almost got it.

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

The following code works great to build templated user controls and templated server controls however, if you would like to add binding capabilities, the process is slightly different, take a look

Download Source Code

This is the code to create a templated UserControl.

Simple Output

Template Container

public class MyTemplateContainer : Control, INamingContainer { }

ASPX Code behind

protected void Page_Load(object sender, EventArgs e)
{
    // just to demonstrate using the contorl
    this.WebUserControl1.Controls.Add(new LiteralControl("<br />new control"));
}

ASPX

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

    <uc1:WebUserControl ID="WebUserControl1" runat="server">
        <ContentTemplate>
            My Template<br />
            <asp:Label Text='Hello People' runat="server" ID="lblMessage" />
        </ContentTemplate>
    </uc1:WebUserControl>

ASCX Code behind

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

ASCX

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

<asp:PlaceHolder runat="server" ID="myPlaceHolderTag" />

Code to add a templated Server Control

Output

ASPX

<%@ Register Namespace="MyControls" TagPrefix="my" %>

<my:MyServerControl runat="server" ID="myServerControl">
    <ContentTemplate>
        My Server templated control<br />
        <asp:Label Text="My Label" runat="server" />
    </ContentTemplate>
</my:MyServerControl>

Template Container

namespace MyControls
{
    [ToolboxItem(false)]
    public class MyTemplateContainer : Control, INamingContainer { } 
}

Templated Server Control

namespace MyControls
{
    [ToolboxData("<{0}:MyServerControl runat=server >")]
    [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;
            }
        }
    }
}

References:

  • How to: Create Templated ASP.NET User Controls

  • Building Templated Custom ASP.NET Server Controls

  • Building DataBound Templated Custom ASP.NET Server Controls



来源:https://stackoverflow.com/questions/11658053/how-to-add-a-templating-to-a-usercontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!