HTML Server Controls in ASP.NET With Examples

In ASP.NET, we may convert an HTML element to an HTML server control by adding an attribute runat=“server”. This notifies that the ASP Engine to create an instance of the control during parsing. We need to specify an ID of the element so that we can manipulate it programmatically at the server-side. These controls are particularly useful for migrating ASP applications to ASP.NET applications.

HTML server controls have been derived directly or indirectly from the base class

System.Web.UI.HtmlControls.HtmlControl
and it maps directly to HTML elements.

HTML Server Controls in ASP.NET With Examples

Basically, the hierarchy divides the classes into three major categories: the classes that mimic the HTML <input> tag, the classes that may act as container classes, and finally the HtmlImage class. Several classes in the second category also employ the HTML <input> tag. HTML server controls must reside within a containing <form> control with the runat=“server” attribute.

HtmlAnchor Control:

The HtmlAchor control (<a>) is used to navigate from a page to another page. It basically works almost like the Html anchor tag, the only difference is that it works on the server. It has the following attributes:

<a runat="server" id="programmaticID" href= "linkurl" name="bookmarkname" OnServerClick="onserverclickhandler" target="linkedcontentframeorwindow" title="titledisplayedbybrowser">

HtmlTable Control:

The HtmlTable control mimics the Html <table> tag. We may define rows using <tr> tags. Table cells are defined using <td> tags. This control is a container control, and so we can embed other controls in its cells. It has the following attributes:

<table runat="server" id="programmaticID" align=left | center | right
bgcolor="bgcolor" border="borderwidthinpixels"
bordercolor="bordercolor" cellpadding="spacingwithincellsinpixels"
cellspacing="spacingbetweencellsinpixels" height="tableheight"
rows="collectionofrows" width="tablewidth" >
</table>