自定义模板化 WebControls - 内部控件始终为 null
我正在为 asp .net Web 表单应用程序构建模板化 Web 控件。大部分运行良好,但我遇到的一个问题是模板内的所有控件在运行时均为空。
例如,如果我的控件标记如下:
<fscc:CollapsiblePanel runat="server" ID="cpExample1" ImageControlId="image1" CssClass="curved">
<Header>
this is my header
<asp:Image ImageUrl="imageurl" runat="server" ID="image1" />
</Header>
<Body>
this is the body
</Body>
</fscc:CollapsiblePanel>
我的页面加载如下:
protected void Page_Load(object sender, EventArgs e)
{
image1.ImageUrl = Paths.Images.expand_jpg;
}
它将编译并运行,但 image1 始终为 null。
我已将这些属性添加到类中:
[ParseChildren(true), PersistChildren(false)]
public class MyControl : WebControl
在模板属性中,我添加了这些属性:
[Browsable(false),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateInstance(TemplateInstance.Single)]
public virtual ITemplate Header { get; set; }
据我所知,这足以让 image1 控件可用。此外,该工具似乎可以识别控件并将 image1 放入 .designer.cs 文件中,但它始终为空。我缺少什么?
I'm building a templated web control for an asp .net web forms application. Most of it runs fine, but one problem I’m stuck with is that all the controls inside my template are null at runtime.
For instance if my control is marked up like this:
<fscc:CollapsiblePanel runat="server" ID="cpExample1" ImageControlId="image1" CssClass="curved">
<Header>
this is my header
<asp:Image ImageUrl="imageurl" runat="server" ID="image1" />
</Header>
<Body>
this is the body
</Body>
</fscc:CollapsiblePanel>
And my page load is like this:
protected void Page_Load(object sender, EventArgs e)
{
image1.ImageUrl = Paths.Images.expand_jpg;
}
It will compile and run, but image1 is always null.
I have added these attributes to the class:
[ParseChildren(true), PersistChildren(false)]
public class MyControl : WebControl
And to the template property I have added these:
[Browsable(false),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateInstance(TemplateInstance.Single)]
public virtual ITemplate Header { get; set; }
So far as I can tell this is enough for the image1 control to be available. Also the tooling seems to recognise control and puts the image1 into the .designer.cs file, but it is always null. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论