Article
Beginning ASP.NET Using VB.NET - Chapter 14: ASP.NET Server Controls
HTML Server Controls
HTML server controls correspond directly to various HTML tags, and are defined within the System.Web.UI.HtmlControls namespace. These controls derive their functionality from the System.Web.UI.HtmlControls.HtmlControl base class. Microsoft provides this suite of HTML server controls for a couple of reasons:
- Some web developers may prefer to work with the HTML-style of control that they're used to
- Developers can convert existing HTML tags to HTML server controls fairly easily, and thus gain some server-side programmatic access to the control
The following HTML tag declaration is, believe it or not, a fully qualified HTML server control that can be accessed programmatically on the server within your web form's code:
<INPUT id="MyHTMLTextBox" type="text" name="MyHTMLTextBox"
runat="server">
What makes this a programmable HTML server control? Simply the reference to runat="server. In this example, using runat="server" ensures that the .NET Framework will convert the HTML tag into a corresponding HtmlInputText object (there is an HTML server control object for every corresponding HTML tag). We also add id="MyHTMLTextBox", to provide a unique name of the object so that we can reference it in our server-side code. Like ASP.NET server controls, HTML server controls offer a variety of features, which include:
- Programmatic Object Model -- you can access HTML server controls programmatically on the server using all the familiar object oriented techniques. Each HTML server control is an object and, as such, you can access its various properties and get/set them programmatically.
- Event Processing -- HTML server controls provide a mechanism to write event handlers in much the same way you would for a client-based form. The only difference is that the event is handled in the server code.
- Automatic Value Caching -- when form data is posted to the server, the values that the user entered into the HTML server controls are automatically maintained when the page is sent back to the browser.
- Data Binding -- it's possible to bind data to one or more properties of an HTML server control.
- Custom Attributes -- You can add any attributes you need to an HTML server control. The .NET Framework will render them to the client browser without any changes. This enables you to add browser-specific attributes to your controls.
- Validation -- you can actually assign an ASP.NET validation control to do the work of validating an HTML server control. Validation controls are covered in detail later in this chapter.
One reason you might consider using HTML server controls in your own web form pages, is to leverage an existing HTML page's HTML tag or code base. For example, let's say you have an existing HMTL page that you would rather not re-write from scratch, but still would like to write some server-side code to access various properties of the various controls on the page. Converting an existing HTML page's controls to HTML server controls is simple: you just add the runat="server" attribute within the tag declaration of the HTML control. You might also need to add an id="MyControl" reference, where "MyControl" is your unique naming identifier for this object so that you can reference the object in your server-side code.
HTML Server Controls vs. ASP.NET Server Controls
Given that Microsoft has provided two distinct categories of server controls (HTML and ASP.NET server controls), with both sets of controls sharing some degree of overlapping functionality, you may be a bit confused as to which set of controls you should use within your web forms. The short answer is simply this: you can use both! It's perfectly okay to mix the usage of HTML server controls and ASP.NET server controls within your web forms -- using one set of controls does not restrict you to that control type. Depending on your preferences and web page requirements, there may, however, be reasons to choose one set over the other. Despite the overlap in functionality, there are some clear distinctions between these controls that you should be aware of when developing your ASP.NET web form pages (we'll be covering the ASP.NET behaviors listed below throughout this chapter):

