在 ITemplated 自定义控件中查找控件
我创建了一个自定义服务器控件,其属性实现了 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 区域添加和删除控件。 最好的方法是什么?
I have created a custom server control with properties implementing the ITemplate interface. It is basically a custom panel box with a header, body & footer. Here is the code:
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; }
}
}
}
It works perfectly but one cannot really access controls within the ITemplates from the code behind. I would like to be able to add and remove controls from the ITemplate regions of my custom control. What would be the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试container.findcontrol("")
you can try container.findcontrol("")