Server Controls Are a Blessing
Posted in Design
I wanted to clarify a thing or two in a follow-up to my recent post, Back To Basics: HTML. A reader sent me an email asking if it was all right to use server controls despite their somewhat messy output. I appreciate the question, and I think it'll come up more than once which is why I'd like to dispel any misunderstanding: server controls are a blessing compared to include files and COM objects from the ASP days. In ASP.NET we can't live without server controls, because even the Page class all web forms ultimately derive from, is one big control, too.
Server controls should not be abused either. Where they are not needed, save the overhead and use plain HTML. For example:
<p><a href="http://mysite.com/signup.aspx">Sign up now</a> for an weekly newsletter!</p>
In this case, should we leave the anchor tag, <a>, or plug in a Hyperlink control? We can assume that this is a plain hyperlink to another page which collects some user information. Therefore, no server-side processing is necessary and we don't need the overhead of a server control. A plain anchor it is.
Here's another example:
<div> <asp:TextBox id="SearchPhrase" runat="server" /> <asp:Button id="RunSearch" runat="server" Text="Search" /> </div>
Some simple search form. Clearly, we want to access the text in the text field without all the sweat of parsing the POST values on our own. Let the framework take care of maintaining postback data handing, view state and so forth. In this case we do need server-side processing, because—most likely—we'll run a search through the database in response to the Search button click.
Here's my rule of thumb: if a piece of code will not be accessed by any server-side logic (setting/getting properties, data binding, etc) or raise events for processing on the server (clicks, data binding, etc) I leave it as plain HTML. There's no need to take every <img> and <a> elements and convert them to Image and Hyperlink server controls without dire need.
6 comments
Payton Byrd
on September 10, 2004
This is really good advice, and I do these things myself. I've even managed to convince a colleage who was always using Request.Form[] to start using the controls server side to eliminate the possibility of introducing bugs by mistyping the key of a Form variable.
ASP.Net gives you so many ways to write great code, I'm surprised there has been so much resistance to server controls.
Ezequiel Espíndola
on September 11, 2004
In the first example, is there a way to overcome the need of web controls when creating a site in multiple languages? How do you manage internationalization with plain HTML?
I believe that if you site needs to go international you would probably be using more web controls than HTML, for anything other than images.
Milan Negovan
on September 11, 2004
If you are handling localization "the ASP.NET way", i.e. with satellite assemblies and a resource manager, then using server controls all over the place might be justified because their contents need to be read from a database and populated on the server.
Our main product at work is fully localizable, but to get to this point has been a pain in the neck because we had to user server controls (even if they are plain LiteralControl) everywhere.
Remember, too: you can always turn an HTML element into a server control by declaring it with the "runat=server" attribute. VS.NET will add a declaration of an equivalent server control from the System.Web.UI.HtmlControls namespace... but! The biggest problem I have with this is that every div becomes a table thanks to adaptive rendering, which is disappointing.
Alejandro
on May 19, 2006
I do currently try to use html controls if there's no need for a server control, yet I allways wondered... how much IS the overhead of server controls? when it's better to use html controls... HOW MUCH better is it? if I have a hyperlink, what's the cost of using a LinkButton or a HyperLink in stead of a "a href" tag??
Milan Negovan
on May 19, 2006
The overhead is almost non-existent, and there's no "better" or "worse" criteria. It's only a matter of convenience.
If you need to access an HTML control on the server, then you tack runat="server" to it, as the last paragraph of this post points out.

Humberto Oliveira
on September 6, 2004
I couldn't agree more with you. In addition to that, the easiness to create server controls are even more blessing. Not satisfied with a control? Create a new one and overrides the Render method and you're done. Isn't it amazingly easy?