62,075
社区成员
发帖
与我相关
我的任务
分享
<YK:Header ID="Header" runat="server" MyHtml="...这里为我一般HTML值" />
<YK:Header ID="Header" runat="server">
<MyTemplate Key="MyHTML">
... 这里为我的一般HTML值
</MyTemplate>
</YK:Header>
public partial class UserControl_MyTemplateUserControl : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (MyTemplate != null)
{
Control ctrl = new Control();
MyTemplate.InstantiateIn(ctrl);
Controls.Add(ctrl);
}
}
[TemplateContainer(typeof(UserControl_MyTemplateUserControl))]
public ITemplate MyTemplate
{
get;
set;
}
}
<%@ Register Src="MyTemplateUserControl.ascx" TagName="MyTemplateUserControl" TagPrefix="uc" %>
<uc:MyTemplateUserControl ID="mtuc" runat="server">
<mytemplate>
<fieldset>
<legend>CustomTemplateControl </legend>
<br />
<div>
我是div</div>
<hr />
<span>我是span</span>
</fieldset>
</mytemplate>
</uc:MyTemplateUserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
namespace CustomWebControl
{
public class CustomTemplateControl : CompositeControl
{
#region//声明模板
[Browsable(false)]
[TemplateContainer(typeof(CustomTemplateControl))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate MyTemplate
{
get;
set;
}
#endregion
protected override void CreateChildControls()
{
Controls.Clear();
if (MyTemplate != null)
MyTemplate.InstantiateIn(this);
else
base.CreateChildControls();
ChildControlsCreated = true;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="UserControl_Default2" %>
<%@ Register TagPrefix="custom" Assembly="CustomWebControl" Namespace="CustomWebControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<custom:CustomTemplateControl ID="ctc" runat="server">
<MyTemplate>
<fieldset>
<legend>CustomTemplateControl </legend>
<br />
<div>
我是div</div>
<hr />
<span>我是span</span>
</fieldset>
</MyTemplate>
</custom:CustomTemplateControl>
</form>
</body>
</html>