Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the runat server attribute

Tags:

asp.net

I'm really new to ASP.NET. I was just checking out a default ASP.NET web application. It comes by default with a few pages (Default.aspx, About.aspx etc).

I noticed that the Site.master file is the file where i create the main layout for my pages. But i also noticed that the head tag has a runat="server" attribute.

I know this tag is used in <asp:XXX> tags, but why in a <head> tag???

Also, when i remove that attribute, then all of the styles are gone from the webpage. So appearently it's doing something. I just don't understand what its exactly doing...

So why is it there, on an HTML tag...??? I don't see any code in there that should be run on the server...

<head runat="server">     <title>Hallo</title>     <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />      <!-- This part is run on the server. So why does the head tag          also need a runat=server ?? -->     <asp:ContentPlaceHolder ID="HeadContent" runat="server">     </asp:ContentPlaceHolder> </head> 
like image 391
Vivendi Avatar asked Jul 16 '12 18:07

Vivendi


People also ask

Does adding a runat server attribute to an HTML element change anything?

HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control.

What is the meaning of this code script runat server </ script?

The runat="server" tag tells the . NET compiler to execute the tag on the server. it can be added to any html tags which make them available on server side code.

What is form runat server?

The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts. In the following example we declare an HtmlAnchor server control in an .aspx file.

What is runat command?

DESCRIPTION. The runat utility is used to execute shell commands in a file's hidden attribute directory. Effectively, this utility changes the current working directory to be the hidden attribute directory associated with the file argument and then executes the specified command in the bourne shell ( /bin/sh ).


2 Answers

You asked why the styles are not applied anymore when removing the runat="server" from the<head> element.

It is simple: by running on the server side, the parser will replace the ~/ from the stylesheet declaration <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" /> with the root path of the application.

The ~ is illegal in a URL. Thus, if this is not replaced by the parser, the file will not be found thus the stylesheet will not be applied.

Oh, btw, setting the runat="server" attribute on the <head> element will force all its sub-elements to be run on the server, thus why the <link> element is run on the server.

like image 139
xypho Avatar answered Oct 11 '22 17:10

xypho


The head element contains a runat="server" attribute, which indicates that it is a server control (rather than static HTML). All ASP.NET pages derive from the Page class, which is located in the System.Web.UI namespace. This class contains a Header property that provides access to the page's region. Using the Header property we can set an ASP.NET page's title or add additional markup to the rendered section. It is possible, then, to customize a content page's element by writing a bit of code in the page's Page_Load event handler.

' Programmatically add a <meta> element to the Header  Dim keywords As New HtmlMeta() keywords.Name = "keywords" keywords.Content = "master page,asp.net,tutorial" Page.Header.Controls.Add(keywords) 

For more info see Specifying Meta Tags in ASP.NET with VB.NET.

like image 40
Joe Avatar answered Oct 11 '22 17:10

Joe