• Welcome to Valhalla Legends Archive.
 

[asp.net] Runat Attribute

Started by Imperceptus, October 08, 2009, 03:30 PM

Previous topic - Next topic

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.
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

MyndFyre

Quote from: Imperceptus on October 08, 2009, 03:30 PM
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.

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:


<!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>


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? :)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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?
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

MyndFyre

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:

<!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">


Now consider this:

<!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">


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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.