Server Controls Are a Blessing
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
<a> elements and convert them to
Hyperlink server controls without dire need.