将控件添加到 UpdatePanel 后,仍然可以从包含页面的代码访问它。这是如何实现的?自定义控件开发人员可以这样做吗?
换句话说,如果我使用 ITemplate 接口(interface)开发模板控件,是否有办法将其连接起来(就像 UpdatePanel 似乎一样),以便(以声明方式)包含的任何控件也可以从包含页面访问?
最佳答案
您可以添加 TemplateInstanceAttribute在您的 ITemplate 属性上实现此目的,它具有将控件提升到页面级别的效果:
[TemplateInstance(TemplateInstance.Single)]
public ITemplate AnonymousTemplate {
get { ... }
set { ... }
}
来自 MSDN:
The TemplateInstanceAttribute class allows you to mark a template property as one that allows single or multiple instantiations. A template that only allows a single instantiation can have the controls that are contained inside of it referenced. The ZoneTemplate property is an example of a property that can be instantiated only one time.
关于asp.net - 有没有比 FindControl() 更好的方法来访问 ITemplate 中的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237495/