尝试包装子控件时出现分析器错误

时间:2009-06-09 11:50:32

标签: asp.net sharepoint moss web-controls

我开发了一个从WebControl继承的服务器控件,它包装了任意数量的子控件并更改了它们的输出。控件类非常简单,只包含RenderContents方法。

以下是如何将其放置在页面上的示例。 (不包括:控制命名空间的注册。)这里的意图是RichImageField控件的渲染输出将被更改:

<RX:HideWhitespaceControl runat="server">
    <PublishingWebControls:RichImageField
        FieldName="PublishingPageImage"
        runat="server"
        id="PageImage">
    </PublishingWebControls:RichImageField>
</RX:HideWhitespaceControl>

但是,当我尝试浏览到该页面时,我的控件类中的代码似乎都没有执行,我收到以下错误:

  

分析程序错误消息:键入   'RX.SharePoint.Common.Controls.HideWhitespaceControl'   没有名为的公共财产   'RichImageField'。

我对此错误出现的原因感到困惑。确实没有名为RichImageField的公共属性,因为这不是属性而是子控件!

我的自定义控件正在页面布局上的SharePoint发布网站中使用,因此我不确定此错误是否来自SharePoint。但它看起来像是一个基本的ASP.NET错误,所以我错过了什么?

2 个答案:

答案 0 :(得分:1)

也许您需要将ParseChildren(false),PersistChildren(true)属性添加到自定义控件中,例如:

[ParseChildren(false)]
[PersistChildren(true)]
public class YourControl : WebControl

答案 1 :(得分:0)

您需要覆盖AddParsedSubObject(object obj)方法来处理子元素:

protected override void AddParsedSubObject(object obj)
{
    if (obj is LiteralControl)
    {
        // This is pure HTML or text...
    }
    else if (...)
    {
        // Handle ASP.NET controls...
    }
}