作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题之前有人问过
How do i add templating to a
UserControl
in ASP.net?
UserControl
开始5,我会调用Contoso
:public partial class Contoso: System.Web.UI.UserControl
{
}
<Contoso>
Stuff in here
<Contoso>
ContentTemplate
ITemplate
类型的属性:public partial class Contoso: System.Web.UI.UserControl
{
public ITemplate ContentTemplate { get; set; }
}
ContentTemplate
属性: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; }
}
<ContentTemplate>
到我们的aspx文件中的控件:1<Contoso>
<ContentTemplate>
Stuff in here
</ContentTemplate>
</Contoso>
ContentTemplate
东西,通过在某处添加它。我们通过将它添加到我们的 UserControl 的内部 div
之一来做到这一点。元素。.aspx
开始最初为空的文件:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contoso.aspx.cs" Inherits="Contoso" %>
div
这将容纳我们的ContentTemplate
东西:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contoso.aspx.cs" Inherits="Contoso" %>
<div id="ContentDiv" runat="server"></div>
ContentTemplate
塞进那个 parent div
在控制的初始化 :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; }
}
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; }
}
INamingContainer
接口(interface)没有任何成员,仅用于标记您的UserControl
类的东西。 <%@ Register src="Contoso.ascx" TagName="Contoso" tagprefix="uc" %>
Contoso.ascx
是 ascx
的名称文件 Contoso
是我们将用来引用此用户控件的元素的名称 uc
是我们必须放在 uc:Contoso
前面的一段文字(我使用 uc
作为用户控制的缩写)<uc:Contoso ID="Crackers" runat="server">
<ContentTemplate>
Stuff goes here
</ContentTemplate>
</qwerty:Contoso>
Error Creating Control - Crackers
Type 'System.Web.UI.UserControl' does not have a public property named 'ContentTemplate'
UserControl
没有名为
ContentTemplate
的公共(public)属性- 所以我很难责怪它。
最佳答案
好吧,我相信你几乎明白了。
顺便提一句。 UserControl 不是使用 Visual Studio 设计器呈现的,但是当您运行应用程序时,该控件可以工作。如果您改用服务器控件,情况会有所不同,在这种情况下,控件会在 Visual Studio 设计器中正确显示
以下代码非常适合构建 模板化用户控件和模板化服务器控件但是,如果您想添加绑定(bind)功能,则过程略有不同,take a look
Download Source Code
这是创建模板 UserControl
的代码.
简单输出
模板容器
public class MyTemplateContainer : Control, INamingContainer { }
protected void Page_Load(object sender, EventArgs e)
{
// just to demonstrate using the contorl
this.WebUserControl1.Controls.Add(new LiteralControl("<br />new control"));
}
<%@ 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>
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"));
}
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:PlaceHolder runat="server" ID="myPlaceHolderTag" />
<%@ 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>
namespace MyControls
{
[ToolboxItem(false)]
public class MyTemplateContainer : Control, INamingContainer { }
}
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;
}
}
}
}
关于asp.net - 如何将模板添加到用户控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658053/
我是一名优秀的程序员,十分优秀!