|I l @ ve RuBoard|
Before we get into the subtleties of HtmlControls and WebControls, we should really explain what a server control is. All server controls inherit from the System.Web.UI.Control class in the .NET Framework. As such, they share a common set of methods and properties that describe how all controls behave. In fact, all of the controls described in the next several chapters share a common heritage: They all inherit from the System.Web. UI.Control class.
For those of you who have never developed with Visual Basic or a similar application, the concept of controls might be new to you. Basically, a control is a programmatic object that is rendered as a part of the user interface (UI). With the right development application, such as Visual Studio .NET, you can create Web pages using controls simply by dragging and dropping controls from a toolbox onto the page. Further, you can modify all of the properties of a control programmatically, so for instance, if you need to set the value of a text box, you can do it from anywhere in your code, rather than having to add ASP script within the HTML of the <input> tag. There are many advantages to this, including the ability to keep all programming logic separate from HTML code, which allows greater reuse and makes it easier for teams of developers and graphic designers to work together.
The simplest controls provided with ASP.NET are the HtmlControls and WebControls described in this chapter. These controls, for the most part, simply render HTML with minimal bells and whistles. The more advanced server controls will be covered in later chapters, as will building your own custom controls.
All of the samples in this chapter will be in C#. The VB.NET versions of the samples can be viewed at http://aspauthors.com/aspnetbyexample/ch05/.
Why Two Kinds of Controls?
Because HtmlControls and WebControls can both render the same standard HTML tags (for instance, a <form> </form> tag), why were both included in ASP.NET? There are several reasons. As we shall see, there are many cases in which either an Html or a Web Control will perform equally well on a page (or WebForm, as ASP.NET pages are often called). The chief differences between the two kinds of controls lie in their syntax and object model. The HtmlControl controls are designed to map as closely as possible to actual HTML tags, generally on a one-to-one basis. This means that, for instance, there are separate HtmlControls for text boxes ( <input type=text> ) and textareas ( <textarea></textarea> ).
WebControls, on the other hand, use a more consistent object model that groups like controls together and differentiates between them using properties (for instance, because the only difference between an <input type=text> and a <textarea> tag is that a textarea allows multiple lines of input, the TextBox WebControl includes a property TextMode that can be set to MultiLine ).
The WebControls tend to have more features and a more intuitive object model than the HtmlControls. However, the beauty of HtmlControls is that they can be created directly from existing HTML tags simply by adding the runat ="server" attribute. This makes it very easy to update existing HTML and to allow Web page design software to edit ASP.NET pages without choking on the custom syntax required for WebControls.
When Should I Use Each Kind of Control?
Generally, WebControls are more feature-rich and have a more consistent object model than the HtmlControls. However, if you are more familiar with HTML tags than with VB controls, you might find the HtmlControls easier to work with. If you need to programmatically alter the value of an HTML tag that doesn't have a corresponding WebControl, such as the <title> of a page, using an HtmlControl would be the easiest way to go. Also, if you need to work with graphic page designers who are using tools that understand HTML tags but not ASP.NET controls, you might find it easier to work with HtmlControls.
|I l @ ve RuBoard|