In an ASP.NET page, the runat attribute determines what will become of a piece of markup text. The text can be emitted at render time verbatim or transformed into a stateful instance of a .NET control class. All markup elements that have the runat attribute set to a value of server are considered declarations of server-side controls and not just simple text. As such, they are instantiated and programmed when the page is processed.
The markup elements whose name matches an HTML element are mapped to the corresponding HTML server control in the System.Web.UI.HtmlControls namespace. Elements that belong to the <asp> namespace are mapped to web server controls in the System.Web.UI.WebControls namespace. Other markup elements are mapped to the assembly and the class name is declared within the page using a @Register directive.
The runat attribute can be used with virtually any HTML tag including page-wide tags such as <title>, <link>, and <body>. These tags aren't rendered with a specific server-side control, but are represented through an instance of the HtmlGenericControl class. HtmlGenericControl is the .NET class used to represent an HTML server-side tag not directly represented by a .NET Framework class. The list of such tags also includes <span>, <div>, <font>, and <iframe>.
The ASP.NET object model doesn't provide methods or properties to set the background or the title of a page programmatically. As the following code demonstrates, though, you can work around the issue with the wise use of the runat attribute.
<%@ Page Language="C#" %> <script runat="server"> void Page_Load(object sender, EventArgs e) {<br> TheTitle.InnerText = "Hello, world";<br> TheBody.Attributes["bgcolor"] = "darkkhaki";<br>} </script> <html><br><title id="TheTitle" runat="server" /> <br><body id="TheBody" runat="server" ><br>Click the <b>View|Source</b> menu item...<br></body><br></html>
The resulting HTML code is:
<html><br><title id="TheTitle">Hello, world</title> <br><body id="TheBody" bgcolor="darkkhaki"><br>Click the <b>View|Source</b> menu item...<br></body><br></html>
In addition to the runat attribute, you need to give the <body> and the <title> tag a unique ID that ASP.NET will use to unambiguously identify the control. Next, you use the InnerText property to set the content of the <title> tag and the Attributes collection to set the background color. Likewise, you can set any of the properties of the <link> tag, thus deciding programmatically, say, which stylesheet to use for the page.
The runat attribute can also be applied to the <meta> tag, but a little trick is needed this time. In HTML, you normally add the <meta> tag without a closing tag. This is no longer acceptable in ASP.NET when the runat attribute is added. The ASP.NET parser, in fact, always checks the markup text for well-formed XML and hence scans the whole file looking for the closing </meta> tag; if the tag is not found, a compile error is generated.
<meta id="meta1" runat="server" http-equiv="refresh" />
In case of unknown tags, namely tags that are neither predefined nor user-defined, the ASP.NET run time can behave in two different ways. If the tag doesn't contain namespace information, then ASP.NET treats it like a generic HTML control. The empty namespace, in fact, evaluates the HTML namespace thereby leading the ASP.NET run time to believe that the tag is really an HTML element. No exception is raised and markup text is generated on the server.
If the tag does contain namespace information, it is acceptable as long as the namespace is <asp> or a namespace explicitly associated with the tag name using a @Register directive. If the namespace is unknown, a compile error occurs.
Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Magazine and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].