Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Exploring the Runat Attribute


Exploring the Runat Attribute

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].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.