When to override Render

A

Allan Ebdrup

We're pretty new to ASP.Net 2.0 and we're having a discussion about best
practice when developing custom server web controls.
I can see that in for example the Wizard control the table contained within
is a control and all the children are controls.
That seems to be a good practice to me, use controls when possible.
On the other hand one of the developers here wants to override the Render
methods and create child tables with strings and a HtmlTextWriter.
In my view that introduces a lot of problems since you can easily make a
mistake in the Html you output, and you don't get any programatical access
to all the children that you have if you use controls.
On the other hand the developer here thinks that all those controls make a
mess, he just want the children of the control to be the stuff that actually
matters, not stuff like tables that are just there for layout purposes.

What's your take on this? What's a good practice? Use controls whenever
possible and only override the Render methods when you absolutely have to?
Or override the render methods every time you can?

Kind Regards,
Allan Ebdrup
 
P

Peter Bucher [MVP]

Hi John, hi Allan
Controls whenever possible. Overriding render should be done only when you
need to, just like developing any code.
I agree with you: when you need to!

Its important to know, whats really goin on.
Use .NET Reflector for this.

The Render Methode looks like:
-----
protected internal override void Render(HtmlTextWriter writer)
{
this.RenderBeginTag(writer);
this.RenderContents(writer);
this.RenderEndTag(writer);
}
-----

As you`ll see, overwriting the Render Methode prevent the execution of these
three methods.
If you got a Tag around your Control, then better overwrite these three or
one of these, if needed.
Otherwise inherit from Control and overwrite the Render Method.
 
W

Walter Wang [MSFT]

Hi Allan,

I agree with John and Peter here.

For some crash courses on developing ASP.NET custom server controls, you
may find following articles useful:

#Creating Controls
http://msdn2.microsoft.com/en-us/library/aa530687.aspx


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top