views:

191

answers:

2

I have developed a server control inherited from WebControl that wraps any number of child controls and changes their output. The control class is very simple and only contains the RenderContents method.

Here is an example of how it has been placed on the page. (Not included: registration of control namespaces.) The intention here is that the rendered output from the RichImageField control will be changed:

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

However when I try to browse to the page none of the code in my control class appears to execute and I receive the following error:

Parser Error Message: Type 'RX.SharePoint.Common.Controls.HideWhitespaceControl' does not have a public property named 'RichImageField'.

I'm confused about why this error is appearing. There is indeed no public property named RichImageField as this is not a property but rather a child control!

My custom control is being used in a SharePoint publishing site on a page layout so I'm not sure if this error is coming from SharePoint. But it looks like a basic ASP.NET error so what am I missing?

A: 

You need to override the AddParsedSubObject(object obj) method to handle child elements:

protected override void AddParsedSubObject(object obj)
{
    if (obj is LiteralControl)
    {
        // This is pure HTML or text...
    }
    else if (...)
    {
        // Handle ASP.NET controls...
    }
}
Blixt
I'm learning some good stuff but still no luck! The code in my control's not executing - will update the question.
Alex Angas
Did you override the AddParsedSubObject method without calling the base.AddParsedSubObject method? Then you should be rid of the "no public property named ..." error. Beyond that I can't be of much more help, other than to say that you'll probably want to register the PreRender event or some other event that is executed after all child controls have been rendered, so that you can get their rendered content and remove whitespace.
Blixt
It didn't work, but thank you for your effort.
Alex Angas
Strange, because I made my own control and it worked fine and let me modify the controls that were inside my control. Try inheriting from Control and overriding the method, maybe that will work better?
Blixt
+1  A: 

Maybe you need to add the, ParseChildren(false), PersistChildren(true) attributes to your custom control, like:

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