Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use runat="server" on normal HTML

Tags:

Is it ever appropriate to use runat="server" on a standard HTML element instead of a true ASP.NET control? I have full control over setting the html/text of the normal element, so why wouldn't I use it instead of a "clunky" ASP.NET WebForms control?

If one is better than the other, some points of interest I would like to know:

  • Performance differences
  • Functionality differences
  • Other differences not so obvious?

An example difference:

<asp:Literal ID="mySpecialHtml" runat="server" />  <div id="mySpecialHtml" runat="server" /> 
like image 615
Benny Avatar asked Jul 12 '12 20:07

Benny


People also ask

Why do we use runat server?

The runat="server" tag in ASP.NET allows the ability to convert/treat most any HTML element as a server-side control that you can manipulate via code at generation time. Some controls have explicit implementations, others simply revert to a generic control implementation.

What is the purpose of the runat server attribute value pair when used with an HTML element?

This attribute serves as an identity for the element and enables you to program to elements by their specific IDs. In addition to this attribute, the HTML element must contain runat="server". This tells the processing server that the tag is processed on the server and is not to be considered a traditional HTML element.

What is runat server in HTML?

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.


2 Answers

Both are ASP.NET server controls. The ones corresponding to HTML elements are in the System.Web.UI.HtmlControls namespace, and the web controls are in the System.Web.UI.WebControls namespace.

The HTML controls are more light-weight and correspond exactly to an HTML element, while the web controls have more features and can be rendered as different HTML elements depending on the browser capabilities and the settings of the control.

A HTML control renders as a single HTML element, while a web control is rendered as zero or more HTML elements. The Literal control for example isn't rendered as an element, it only outputs its text. There are other controls that doesn't render any elements by themselves, like the Repeater and PlaceHolder controls. On the other hand, the CheckBoxList control for example is rendered as several HTML element, a table as container, and input elements for each checkbox inside it.

An example of a control that is rendered using different elements is the TextBox control, which will be rendered either as an input or a textarea element depending on its TextMode property.

The web controls have more features, but also uses more resources. They have more properties and support things like themes and data binding. Many of the web controls put data in the ViewState, which is sent as part of the page. If you are not careful, the ViewState can get quite large, and affect the loading time of the page.

like image 120
Guffa Avatar answered Oct 22 '22 08:10

Guffa


The only reason I have used server html controls is when I needed the flexibility of writing my own html but still needed to access it's properties in code behind.

<div id="mySpecialHtml" runat="server" /> 

In code behind:

mySpecialHtml.InnerHtml = "something else"; 
like image 44
jrummell Avatar answered Oct 22 '22 07:10

jrummell