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 el
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.
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.
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";
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.
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 ).