24

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" />
Benny
  • 3,899
  • 8
  • 46
  • 81

5 Answers5

21

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.

Jesuraja
  • 3,774
  • 4
  • 24
  • 48
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
12

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";
jrummell
  • 42,637
  • 17
  • 112
  • 171
5

ASP.NET parser interprets standard html elements w/ runat="server" tag as html server controls. Example:

<a runat="server"></a>

is interpreted as

<asp:HtmlAnchor runat="server"></asp:HtmlAnchor>

So, be aware that if you use runat="server" than you are using corresponding HTML Server control.

Additionally, ASP.NET contains a set of Web Server controls, i.e. HyperLink control, which provide more functionality, more properties, events etc. but can be slower in some cases.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Raman Zhylich
  • 3,537
  • 25
  • 23
  • +1 Some people think that adding a runat="server" to a HTML control changes nothing, when in fact they are converting this control to a server control. – Hanlet Escaño Jul 12 '12 at 21:45
1

I prefer to use html controls when there is more work on client side, and use asp.net controls when there is more work on server side ( code behind ).

Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
1

I believe ASP.NET uses a different set of Web Server controls which usually has a lot more functionality, but it's really up to your preference. If your work is mostly server-side, I'd stick with ASP.NET controls.

jzacharia
  • 492
  • 1
  • 3
  • 14