tags:

views:

4520

answers:

6

Why do I have to specify runat="server" on all my asp.net controls when it is a mandatory attribute and "server" is the only option available in my limited low level asp.net knowledge, and I get an error if I don't use it.

I do understand that I can optionally use it on my html tags, and I do understand the client/server paradigm and what it is actually specifying.

Is it a redundant tag that could just be implied by the control being an asp.net control, or is there an underlying reason?

+5  A: 

Not all controls that can be included in a page must be run at the server. For example:

<INPUT type="submit" runat=server />

This is essentially the same as:

<asp:Button runat=server />

Remove the runat=server tag from the first one and you have a standard HTML button that runs in the browser. There are reasons for and against running a particular control at the server, and there is no way for ASP.NET to "assume" what you want based on the HTML markup you include. It might be possible to "infer" the runat=server for the <asp:XXX /> family of controls, but my guess is that Microsoft would consider that a hack to the markup syntax and ASP.NET engine.

Dave Swersky
+12  A: 

I've always believed it was there more for the understanding that you can mix ASP.NET tags and HTML Tags, and HTML Tags have the option of either being runat="server" or not. It doesn't hurt anything to leave the tag in, and it causes a compiler error to take it out. The more things you imply about web language, the less easy it is for a budding programmer to come in and learn it. That's as good a reason as any to be verbose about tag attributes.

This conversation was had on Mike Schinkel's Blog between himself and Talbot Crowell of Microsoft National Services. The relevant information is below (first paragraph paraphrased due to grammatical errors in source):

[...] but the importance of [runat="server"] is more for consistency and extensibility.

If the developer has to mark some tags (viz. <asp: />) for the ASP.NET Engine to ignore, then there's also the potential issue of namespace collisions among tags and future enhancements. By requiring the runat="server" attribute, this is negated.

It continues:

If [runat=client] was required for all client-side tags, the parser would need to parse all tags and strip out the [runat=client] part.

He continues:

Currently, If my guess is correct, the parser simply ignores all text (tags or no tags) unless it is a tag with the runat=server attribute or a “<%” prefix or ssi “<!– #include(...) Also, since ASP.NET is designed to allow separation of the web designers (foo.aspx) from the web developers (foo.aspx.vb), the web designers can use their own web designer tools to place HTML and client-side JavaScript without having to know about ASP.NET specific tags or attributes.

George Stocker
that first quoted paragraph makes my brain hurt :-(
Orion Edwards
Why, surely "the of one with collision name has future agent user some if What engine" is perfectly understandable :) Me thinks Yoda may be hiding out at Microsoft
johnc
Yea, it's a pain in the rear end because I quoted a quote, so naturally there are levels of indirection that are there that shouldn't be. I'll try to clean it up today.
George Stocker
@Gortok - I guess you never got around to that?
Erik Forbes
Whatever the reason, it's still a PITA having to type it in for every <asp:> tag wghen it could safely be the default value.
belugabob
No, I didn't. Kinda left it on the backburner. Back on my list of things to do.
George Stocker
+1 good anwser, helped me ge the idea of all this
marcgg
Thank you George for cleaning up the text :)
johnc
+3  A: 

My suspicion is that it has to do with how server-side controls are identified during processing. Rather than having to check every control at runtime by name to determine whether server-side processing needs to be done, it does a selection on the internal node representation by tag. The compiler checks to make sure that all controls that require server tags have them during the validation step.

tvanfosson
+1  A: 

If you use it on normal html tags, it means that you can programatically manipulate them in event handlers etc, eg change the href or class of an anchor tag on page load... only do that if you have to, because vanilla html tags go faster.

As far as user controls and server controls, no, they just wont work without them, without having delved into the innards of the aspx preprocessor, couldn't say exactly why, but would take a guess that for probably good reasons, they just wrote the parser that way, looking for things explicitly marked as "do something".

If Jon Skeet is around anywhere, he will probably be able to provide a much better answer.

seanb
+2  A: 

I usually don't like to guess, but I'm going to on this one...

If you remember Microsoft's .NET marketing hype back in the day (2001?), it was hard to tell what .NET even was. Was it a server? a programming platform? a language? something new entirely? Given the ads, it was ambiguously anything you wanted it to be - it just solved any problem you might have.

So, my guess is there was a hidden grand vision that ASP.NET code could run anywhere - server side OR client side, in a copy of Internet Explorer tied to the .NET runtime. runat="server" is just a vestigial remnant, left behind because it's client-side equivalent never made it to production.

Remember those weird ads?

Related: Article from The Register with some .NET history.

Corbin March
+1 for the interesting thoughts
johnc
+1  A: 

It's there because all controls in ASP .NET inherit from System.Web.UI.Control which has the "runat" attribute.

in the class System.Web.UI.HTMLControl, the attribute is not required, however, in the class System.Web.UI.WebControl the attribute is required.

edit: let me be more specific. since asp.net is pretty much an abstract of HTML, the compiler needs some sort of directive so that it knows that specific tag needs to run server-side. if that attribute wasn't there then is wouldn't know to process it on the server first. if it isn't there it assumes it is regular markup and passes it to the client.

Russ Bradberry
Your answer is the very question reformulated.
Pablo Fernandez
My answer was simply stating that the runat attribute is there due to inheritance. My apologies for not being clear.
Russ Bradberry
A little too high in the stack, I'm afraid, my question was regarding why it was there in the first place. Thanks anyway
johnc
edited to redefine what i said
Russ Bradberry
Again, not really answering the question, but I see what you are trying to say
johnc