ASP.NET HTML Server Controls

Monday, April 29, 2013

aspx html server controls

ASP.NET provides several types of server controls to help you with rapid development. Unlike traditional HTML elements, server controls allow you to access different properties using server-side scripting. The HTML server controls are nothing more than Hypertext Markup Language (HTML) elements that include a runat="server" attribute. The HTML server controls have the same HTML output and the same properties as their corresponding HTML elements. In addition, HTML server controls provide automatic state management and server-side events. HTML server controls offer the following advantages over traditional HTML elements in your markup:

  • The HTML server controls map one to one with their corresponding HTML tags.
  • When the ASP.NET application is compiled, the HTML server controls with the runat=server attribute are compiled into the assembly.
  • Most controls include an OnServerEvent for the most commonly used event for the control.
  • When the ASP.NET page is reposted, the HTML server controls keep their values.

So what does this all mean? Basically, by adding the runat="server" attribute, you can manipulate the properties of these controls on the server-side. For example, this will allow you to modify the element's attributes during a page load, or during a click event. While this can be handled client-side using JavaScript, this process also allows you to handle it server-side if needed.



HTML Server Controls

HTML Server ControlDescription
HtmlAnchorControls an <a> HTML element
HtmlButtonControls a <button> HTML element
HtmlFormControls a <form> HTML element
HtmlGenericControls other HTML element not specified by a specific HTML server control, such as <body>, <div>, <span>, etc.
HtmlImageControls an <image> HTML element
HtmlInputButtonControls <input type="button">, <input type="submit">, and <input type="reset"> HTML elements
HtmlInputCheckBoxControls an <input type="checkbox"> HTML element
HtmlInputFileControls an <input type="file"> HTML element
HtmlInputHiddenControls an <input type="hidden"> HTML element
HtmlInputImageControls an <input type="image"> HTML element
HtmlInputRadioButtonControls an <input type="radio"> HTML element
HtmlInputTextControls <input type="text"> and <input type="password"> HTML elements
HtmlSelectControls a <select> HTML element
HtmlTableControls a <table> HTML element
HtmlTableCellControls <th>and <td> HTML elements
HtmlTableRowControls a <tr> HTML element
HtmlTextAreaControls a <textarea> HTML element


Example

In the following example, the markup is located in the .aspx file, and the VB or C# code is either in a script block within the .aspx page or in the code-behind page. The .aspx file contains an HTML Server Control for an anchor element.

ASPX

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

VB

Sub Page_Load(sender As Object, e As EventArgs)
    anchor1.HRef = "http://www.itgeared.com"
End Sub

C#

void Page_Load(object sender, EventArgs e) 
{ 
   anchor1.HRef = "http://www.itgeared.com";
}

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Sams Teach Yourself ASP.NET 4 in 24 Hours Beginning ASP.NET 4: in C# and VB Pro ASP.NET MVC 4