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 Control | Description |
HtmlAnchor | Controls an <a> HTML element |
HtmlButton | Controls a <button> HTML element |
HtmlForm | Controls a <form> HTML element |
HtmlGeneric | Controls other HTML element not specified by a specific HTML server control, such as <body>, <div>, <span>, etc. |
HtmlImage | Controls an <image> HTML element |
HtmlInputButton | Controls <input type="button">, <input type="submit">, and <input type="reset"> HTML elements |
HtmlInputCheckBox | Controls an <input type="checkbox"> HTML element |
HtmlInputFile | Controls an <input type="file"> HTML element |
HtmlInputHidden | Controls an <input type="hidden"> HTML element |
HtmlInputImage | Controls an <input type="image"> HTML element |
HtmlInputRadioButton | Controls an <input type="radio"> HTML element |
HtmlInputText | Controls <input type="text"> and <input type="password"> HTML elements |
HtmlSelect | Controls a <select> HTML element |
HtmlTable | Controls a <table> HTML element |
HtmlTableCell | Controls <th>and <td> HTML elements |
HtmlTableRow | Controls a <tr> HTML element |
HtmlTextArea | Controls 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