Valhalla Legends Forums Archive | .NET Platform | [asp.net] Runat Attribute

AuthorMessageTime
Imperceptus
Why does asp.net require a runat="server" attribute if you can't specify another option like runat="client".  I am a little confused as to why this attribute even exists.
October 8, 2009, 8:30 PM
Myndfyr
[quote author=Imperceptus link=topic=18087.msg183502#msg183502 date=1255033813]
Why does asp.net require a runat="server" attribute if you can't specify another option like runat="client".  I am a little confused as to why this attribute even exists.
[/quote]

Instead of answering your question directly, I'll take you through a guided exercise to see if you can figure it out (and thereby learn something very valuable for ASP.net).

You'll need .NET Reflector.  If you don't use it already, you should be - it's invaluable.

In Visual Studio, create a new Web Site project (it's important that you use Web Site, not Web Application project - they're distinct options on the File->New menu).  A regular ASP.NET Web Site will do.

Open up default.aspx and change the content (excluding the @Page directive, since I'm sure yours will be VB) to:
[code]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <input type="text" id="foo" />
       <asp:TextBox ID="bar" runat="server" />
   </div>
   </form>
</body>
</html>
[/code]

Now, right-click the web site in Solution Explorer and choose Publish Web Site.  Take note of where it's targeted.  Un-check the "Allow this precompiled site to be updatable" and click OK.

Navigate to the folder where you published the site and go into the "bin" directory.  Using Reflector, open up the DLL in there (mine is "App_Web_xehlufg0.dll").  Within Reflector, navigate to the ASP namespace, and the default_aspx class.  Under the default_aspx class, look at the __BuildControlTree(default_aspx) method.

What Visual Studio did in the "Publish Web Site" step is what the ASP.NET runtime engine does in your "Temporary ASP.NET Files" location of your hard drive - and this is the code that is actually running when you create a website.

Does that answer your question? :)
October 9, 2009, 5:53 AM
Imperceptus
Well that was a fun excercise lol. but no.  It doesn't answer my question. 

Why Must i specify "Runat="server"" in a 'server' control when there are no other options.  From a developement side of things I dont understand why the compiler doesn't just assume all server controls to have that attribute, its not like its an option.

If you goto buy a car do you tell the attendent to make sure it has tires everytime?
October 9, 2009, 2:33 PM
Myndfyr
At its heart, ASP.NET is a modified XML parser.  It allows invalid XML, but it also uses the same kind of tokenizer and lexical analysis tool.

Consider a valid XHTML document:
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
[/code]

Now consider this:
[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:asp="http://www.w3.org/2009/asp">
[/code]

I've just created a valid instance in which an "asp" prefix could be used without a runat="server" tag.

It turns out that this isn't unused.  Facebook uses the "fb" prefix for its FBML controls that are used in application canvas, such as <fb:name />. 

So, the runat="server" attribute is the flag to the ASP.NET parsing engine that, hey, you need to execute the server logic for this control, and not just render out the literal text that was there.
October 9, 2009, 7:03 PM

Search