日期:2014-05-18  浏览次数:20400 次

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>