My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
Something's been bugging me lately about ASP.NET: I can't seem to remember to type runat="server" every time I write a line of code to display a server control.

For normal HTML elements, I understand the necessity of using the attribute when you want that element to be tied to the server. But I can't seem to figure out any other way to run a control that's been prefixed with the namespace asp.

So why does ASP.NET require such a silly, useless bit of text for everything it does?

I can think of three reasons:
  1. Initially, Microsoft envisioned the controls running in several places: perhaps the browser, a printer, or a zoo. But three and a half major versions in, we're still required to pass the same attribute with the same value for every control. Couldn't they drop the requirement and make it a default if they decided to add more valid values?

  2. They wanted a constant reminder to novices that this code was running on the server. This way, people wouldn't ask why changes weren't occurring on the browser before posting forms to the server.

  3. They wanted to remain consistent in having to tell the computer everything at least twice, à la Object obj = new Object();. However, in this case, it seems more like Object obj = new Object(type="Object");. I'm not sure why that is.
Can you think of any other possible reasons? Please enlighten me, so I can forgive and forget this minor annoyance.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

My thought is for performance reasons. You can have a control that is not runat="server", and it will not be included for server side processing. For example if you have a static label that will never change text, or something like that.

Posted by Jeremy on Jan 16, 2008 at 08:20 AM UTC - 5 hrs

The problem with that theory is that if you neglect to put the runat="server" in the asp:Label it will compile and run, but it doesn't show up in the page (only the verbatim code, which of course is enclosed in angled brackets, so it doesn't show on the page.)

I guess it's possible to want to use a tag like that in your xml, but I wouldn't call it likely. I wonder if anyone has /ever/ done so.

Posted by Sammy Larbi on Jan 16, 2008 at 09:01 AM UTC - 5 hrs

To be consistent. :)

You can create your own server tags with your own namespace and tag name (such as myLib:NewLabel). Since ASP.NET can't make any assumptions about what is an HTML (or XML) tag, and what is an ASP.NET control you have to tell it.

Also, if I'm not mistaken, you can attribute any HTML tag with runat, and it will create an html element object so you can manipulate it in code. (wooo...)

So, if the asp.net controls didn't follow the same pattern it wouldn't be consistent, because they are implemented the same way as you'd implement your own server controls after all.

At least that's the way I see it.

A comment about your "Object obj = new Object()" comment:
That's because when you declare "Object obj" you're actually declaring a reference to an object of type Object, not really an instance of type Object. Initially it doesn't reference anything (similar to pointers).

Posted by Glitch on Jan 16, 2008 at 11:49 AM UTC - 5 hrs

@Glitch: I was hoping you'd respond.

You are right - it does allow it in an HTML tag (as I meant to get across when I said "For normal HTML elements, I understand the necessity of using the attribute when you want that element to be tied to the server.")

If consistency is the case, (and that is a valid reason I didn't think of) then I wonder why they didn't make you type runat="browser" for the controls that shouldn't run on the server (minus html elements, which should require runat="server" to run there).

I'm being nitpicky, I realize, but it's just been annoying me to have to alt-tab from browser to IDE, enter runat=server in the appropriate spot, hit alt-tab back to browser, and reload, waiting for the recompilation. =)

I'll get used to it and fall in line soon enough.

As for telling the compiler several times what you want (Object obj = new Object()) - I'd prefer type inference to short-term memory loss. Nothing against C# of course - just poking a little fun at the entire lot of languages in the same tradition. =)

Posted by Sammy Larbi on Jan 16, 2008 at 12:24 PM UTC - 5 hrs

How though, would they know what was not an html element, and what was? If I'm not mistaken, modern pages are verified using XHTML which follows the XML spec right? Which I would imagine means you can use namespace prefixes within your XHTML code. So if that's true, you couldn't assume any prefix was guaranteed to be a control only prefix.

Granted, by importing your controls it would know what tag prefixes would be mapped to .NET namespaces, but what happend if your tag prefix collided with a legit XHTML namespace prefix? (I appologize too if my terminology is wrong, I'm not savvy with XML namespaces and such.)

If then a control wasn't found should it error? Or should it just let the control be rendered as-is?

I think, as annoying as it is, supplying the runat="server" allows the .NET parser a more efficient method for determining what tags are controls and what ones aren't. If you runat="server" and a valid .NET implementation isn't found, it's obviously an error. If you don't runat="server", no time is wasted looking at the tag (aside from what is needed that is). :) It sure beats adding runat="client" for all of your non-control tags which probably outnumber control tags significantly.

But I feel your pain. I haven't done a lot of ASP.NET work, but almost every time I do I forget the runat and bang my head on the desk for a minute or two before sighing and realizing my error.

Posted by Glitch on Jan 18, 2008 at 09:55 PM UTC - 5 hrs

You are right. I guess I'm coming from the "opinionated framework" stance where I think they should just treat the asp namespace as their own, and run it automatically.

If someone is using that namespace AND ASP.NET, they are probably crazy. I don't think you should disallow it because it's crazy, however. I think you should do it to make it easier on all the sane folks. =)

Posted by Sammy Larbi on Jan 21, 2008 at 09:15 AM UTC - 5 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and C++ (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner