Question

I would like to override the Render method of WebControl, however instead of HTMLTextWriter, I would like to use my HTMLTextWriterExtension class which inherits from HTMLTextWriter and has a few overrides in it with some functionality I need.

Base Class Render (WebControl)

protected internal override void Render(HtmlTextWriter writer);

Override Class Render (My Classes)

public override void Render(HtmlTextWriterExtension output)

The problem with the above is that this then isn't an override of the Render method, and so isn't called during the render.

I realise it's possible as WebControl has done the same thing as it overrides Control's render method with it's HTMLTextWriter.

I just can't work out how to do it. I'm probably having a mind dump, little bit ill and it's Monday morning.. This should be something easy.

Anyway, thanks for any advice you can give me. Regards.

Was it helpful?

Solution

You just need to override the same signature, then use your writer, then copy content from your writer to original writer:

Base Class Render (WebControl)

protected internal override void Render(HtmlTextWriter writer);

Override Class Render (My Classes)

public override void Render(HtmlTextWriter output)
{
    // string builder is just an example of storage that custom TextWriter uses
    var builder = new StringBuilder();
    var myCustomWriter = new HtmlTextWriterExtension(builder);
    base.Render(myCustomWriter);
    myCustomWriter.Flush();

    // write content written from base class to original writer
    output.Write(builder.ToString()); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top