2.7 HtmlControls


Throughout this chapter, the examples have referenced control classes such as HtmlInputText and HtmlGenericControl , simply stating that they were examples of server-side controls. This section more formally introduces these classes and their compatriots in the HtmlControl hierarchy. It is classes from this hierarchy that your page will work with if you elect to use server-side controls by adding runat =server attributes to existing HTML elements in a form (in contrast to using the syntactically different WebControls , discussed in the next section).

You can mark literally any HTML element in an .aspx file with the runat=server attribute to obtain a server-side version. When you do this, the server-side control that is created to correspond to the client-side HTML element comes from the HtmlControl hierarchy. This hierarchy is shown in Figure 2-7.

Figure 2-7. HtmlControl Hierarchy

graphics/02fig07.gif

Note that all these classes derive from a common base class, System.Web.UI.Control , and, more specifically , from System.Web.UI.HtmlControls.HtmlControl . The Control base class contains functionality and state common to all server-side controls, the details of which are discussed in Chapter 8, where we look at building your own server-side controls. The HtmlControl base class further adds properties and methods common to all HtmlControls . This includes properties such as Style and Disabled . When you create an .aspx file with HTML tags attributed with runat=server , the class chosen from this hierarchy depends on the tag. Table 2-1 lists the various HTML tags and their corresponding HtmlControl -derived classes.

Table 2-1. Tag Mappings for HtmlControls

Tag

HtmlControl Class

<img runat=server/>

HtmlImage

<input type=file runat=server/>

HtmlInputFile

<input type=hidden runat=server/>

HtmlInputHidden

<input type=image runat=server/>

HtmlInputImage

<input type=radio runat=server/>

HtmlInputRadioButton

<input type=text runat=server/>

HtmlInputText

<input type=checkbox runat=server/>

HtmlInputCheckBox

<form runat=server>

HtmlForm

<span runat=server>

<div runat=server>

<p runat=server> etc. (all other elements)

HtmlGenericControl

<select runat=server/>

HtmlSelect

<table runat=server/>

HtmlTable

<td> (within a server-side table)

<th> (within a server-side table)

HtmlTableCell

<tr> (within a server-side table)

HtmlTableRow

<textarea runat=server/>

HtmlTextArea

<a runat=server/>

HtmlAnchor

<input type=button runat=server />

HtmlInputButton

<input type=submit runat=server />

HtmlInputButton

<input type=reset runat=server />

HtmlInputButton

Note that while many of the HTML elements have unique mappings, there is a class of tags that all map to a single control: HtmlGenericControl . The HtmlGenericControl class represents controls that have no unique server-side behavior, such as the span , body , and p elements. This class simply contains a TagName property in addition to the properties it inherits from the HtmlControl base class. The TagName is set to the name of the tag that should be rendered by the control (such as span , body , or p ), and the inherited attribute collection ( Attributes ) is a generic set of name/value pairs that will be rendered as attributes for the tag. Note also that this control derives from the HtmlContainerControl , meaning that it can contain other controls and literal HTML within its tag.

The remainder of the HtmlControls have properties, methods, and events similar to their HTML counterparts, so anyone used to working with HTML should feel comfortable with these controls. For details on each control, refer to the online documentation available through MSDN. [6]

[6] MSDN documentation is available through Visual Studio .NET or at http://msdn.microsoft.com.



Essential ASP.NET With Examples in C#
Essential ASP.NET With Examples in C#
ISBN: 0201760401
EAN: 2147483647
Year: 2003
Pages: 94
Authors: Fritz Onion

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net