July 31 2008

“Error Rendering Control” with a Custom Control in Visual Studio HTML designer

On the project I’m contracting on, there are some Custom Controls that override the Render method.  These controls unfortunately were working perfectly at run-time in the browser, but in the WYSIWYG html designer in Visual Studio, the controls would render just as a grey box with the text

“Error Rendering Control - <control name>
An unhandled exception has occurred.
Object refrence not set to an instance of an object”

Some research showed that our overriding Render method wasn’t working in Design Mode.  The following code placed in the beginning of the Render method detects if the control is being shown in design mode and renders more in a more rudementary fashion.

protected override void Render(HtmlTextWriter writer)
{
    // allows this control to be rendered in the HTML designer
    if (this.DesignMode)
    {
        base.Render(writer);
        return;
    }

... overriding render code here ...

}

Obviously this is a band-aid solution. To get the control rendering in a more complex manner we would have to address the error shown in the last line of the control error, which would take a lot more work.

Comments (View)
blog comments powered by Disqus

Please...

Leave a comment if this has helped or offended you.

StackOverflow Id