0

我创建了一个自定义服务器控件,其中包含实现 ITemplate 接口的属性。它基本上是一个带有页眉、正文和页脚的自定义面板框。这是代码:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ACME.Web.Controls
{
    [ParseChildren(true)]
    [DefaultProperty("Text")]
    [ToolboxData("")]
    public class Panel : WebControl, INamingContainer
    {

        private ITemplate _header = null;
        private Control _headerControl = null;
        private Control _headerContainerControl = null;

        private ITemplate _content = null;
        private Control _contentControl = null;
        private Control _contentContainerControl = null;

        private ITemplate _footer = null;
        private Control _footerControl = null;
        private Control _footerContainerControl = null;

        [PersistenceMode(PersistenceMode.InnerProperty),
          TemplateContainer(typeof(TemplateControl)),
          TemplateInstance(TemplateInstance.Single)]
        public ITemplate Header
        {
            get { return _header; }
            set { _header = value; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty),
          TemplateContainer(typeof(TemplateControl)),
          TemplateInstance(TemplateInstance.Single)]
        public ITemplate Content
        {
            get { return _content; }
            set { _content = value; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty),
          TemplateContainer(typeof(TemplateControl)),
          TemplateInstance(TemplateInstance.Single)]
        public ITemplate Footer
        {
            get { return _footer; }
            set { _footer = value; }
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            if (this.ID == null) this.ID = "panel-" + Guid.NewGuid().ToString();

            this.CssClass = "panel";

            if (this.Header == null)
            {
                this.CssClass += " without-header";
            }
            else
            {
                this.CssClass += " with-header";

                _headerControl = new System.Web.UI.WebControls.Panel { CssClass = "panel-header" };
                _headerControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-l" });
                _headerControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-c" });
                _headerControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-r" });

                _headerControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-header-wrapper" });
                _headerControl.Controls[1].Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "wrapper" });

                _headerContainerControl = new Control();
                if (this.Header != null)
                {
                    Header.InstantiateIn(_headerContainerControl);
                }
                else
                {
                    _headerContainerControl.Controls.Add(new LiteralControl("Test"));
                }

                _headerControl.Controls[1].Controls[0].Controls[0].Controls.Add(_headerContainerControl);
                Controls.Add(_headerControl);
            }

            _contentControl = new System.Web.UI.WebControls.Panel { CssClass = "panel-body" };
            _contentControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t" });
            _contentControl.Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t-l" });
            _contentControl.Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t-c" });
            _contentControl.Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-t-r" });

            _contentControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m" });
            _contentControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m-l" });
            _contentControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m-c" });
            _contentControl.Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-m-r" });

            _contentControl.Controls[1].Controls[1].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-wrapper" });
            _contentControl.Controls[1].Controls[1].Controls[0].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "scroll-wrapper" });

            _contentControl.Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b" });
            _contentControl.Controls[2].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b-l" });
            _contentControl.Controls[2].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b-c" });
            _contentControl.Controls[2].Controls.Add(new System.Web.UI.WebControls.Panel { CssClass = "panel-body-b-r" });

            _contentContainerControl = new Control();
            if (this.Content != null)
            {
                Content.InstantiateIn(_contentContainerControl);
            }
            else
            {
                _contentContainerControl.Controls.Add(new LiteralControl("Test"));
            }

            _contentControl.Controls[1].Controls[1].Controls[0].Controls[0].Controls.Add(_contentContainerControl);
            Controls.Add(_contentControl);

            if (this.Footer == null)
            {
                this.CssClass += " without-footer";
            }
            else
            {
                this.CssClass += " with-footer";
                _footerControl = new System.Web.UI.WebControls.Panel { CssClass = "panel-footer" };

                _footerContainerControl = new Control();
                if (this.Footer != null)
                {
                    Footer.InstantiateIn(_footerContainerControl);
                }
                else
                {
                    _footerContainerControl.Controls.Add(new LiteralControl("Test"));
                }

                _footerControl.Controls.Add(_footerContainerControl);
                Controls.Add(_footerControl);
            }

        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Div; }
        }

    }

}

它运行良好,但无法从后面的代码真正访问 ITemplates 中的控件。我希望能够在自定义控件的 ITemplate 区域中添加和删除控件。最好的方法是什么?

4

1 回答 1

0

你可以试试 container.findcontrol("")

于 2009-07-28T06:54:53.323 回答