当前位置: 代码迷 >> ASP.NET >> ascx (用户控件) 可以做成容器形式么解决办法
  详细解决方案

ascx (用户控件) 可以做成容器形式么解决办法

热度:5336   发布时间:2013-02-25 00:00:00.0
ascx (用户控件) 可以做成容器形式么
希望可以包含其它控件,请高手指点

------解决方案--------------------------------------------------------
我写一个demo:

HTML code
<%@ Control Language="C#" %><script runat="server">    [TemplateContainer(typeof(MyTemplateContainer))]    [PersistenceMode(PersistenceMode .InnerProperty)]    public ITemplate 内容 { get; set; }    [TemplateContainer(typeof(MyTemplateContainer))]    [PersistenceMode(PersistenceMode.InnerProperty)]    public ITemplate 广告 { get; set; }    public class MyTemplateContainer : Control, INamingContainer    {        public MyTemplateContainer(string ID)        {            this.ID = ID;        }    }    protected override void CreateChildControls()    {        base.CreateChildControls();        this.PlaceHolder1.Controls.Clear();        if (this.内容 != null)        {            MyTemplateContainer c = new MyTemplateContainer("content1");            this.内容.InstantiateIn(c);            this.PlaceHolder1.Controls.Add(c);        };        this.PlaceHolder2.Controls.Clear();        if (this.广告 != null)        {            MyTemplateContainer c = new MyTemplateContainer("content2");            this.广告.InstantiateIn(c);            this.PlaceHolder2.Controls.Add(c);        }    }</script><table width="100%">    <tr>        <td>            内容:<br />            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>        </td>        <td>            <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>        </td>    </tr></table>
  相关解决方案