自定义模板WebControls - 内部控制始终为空

时间:2010-11-17 15:15:24

标签: custom-controls asp.net-4.0 webforms

我正在为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文件中,但它始终为null。我错过了什么?

1 个答案:

答案 0 :(得分:2)

在模板化控件上使用[ParseChildren(ChildrenAsProperties = false)]对我有用,如下所述:

Possible to have a inner control on a custom server control?