ASP.NET Web Controls Reference
Web controls provide an abstract, consistent, and strongly typed object model. They are abstract because their object model does not necessarily reflect HTML syntax. These controls include standard controls like text boxes and radio buttons, as well as rich controls like calendars and data grids. Web controls are always declared with the ASP namespace prefix, sometimes using self-closing tags as follows:
<asp:textbox id=”txtMesage” text=”Hello, World!” runat=”server” />
Web developer can alternatively declare a web control by using an opening and closing tag pair. For certain controls, such as the Label and TextBox controls, any text contained between the opening and closing tags will be assigned to the Text property of the control. Thus, the following code fragment is equivalent to the previous one:
<asp:textbox id=”txtMessage” runat=”server”>
The attributes of web controls declared using the ASP. NET syntax become the properties of the control, and Web developer can access them programmatically. Unlike the HTML controls, the object model of the web controls does not necessarily reflect HTML syntax. The main reason for this behavior is that, depending on the attributes applied to a Web control, it may render one of many HTML elements to the browser. For example, <asp:textbox> can render <input type=”text”>, <input type=”password”>, or <textarea>, based on the value of the TextBoxMode attribute supplied by the developer at design time.
ASP.NET HTML Controls Reference
The HTML controls have a one-to-one mapping with HTML tags and Web developer can create HTML controls and change their appearance by modifying their properties. HTML controls have an object model that closely resembles the HTML syntax of the elements, as well as the Dynamic HTML (DHTML) object model.
In ASP.NET there are two groups of HTML controls:
- HTML container controls which support <a>, <img>, <form>, <table>, <tr>,<th>,<td>,<select>,<textarea>,<button>
- HTML input controls which support all <input> tags
Web developer can declare other HTML tags as server-side controls by using the runat=”server” attribute/value pair. However, these controls are not supported; instead, unsupported HTML elements are handled by a generic super HTML server control called HtmlGenericControl. The HTML elements Web developer might typically handle in this way include <div>, <span>, <body>, and <font>.
HTML container controls
The base class for container controls is the HtmlContainerControl class, which descends directly from HtmlControl. The HTML elements addressed by this tag are elements that must have a closing tag—that is, forms, selection boxes, and tables, as well as anchors and text areas. Compared to the HtmlControl class, a container control features a couple of additional string properties—InnerHtml and InnerText.
Both properties manipulate the reading and writing of literal content found between the opening and closing tags of the element. Note that you cannot get the inner content of a control if the content includes server controls. InnerHtml and InnerText work only in the presence of all literal content. The tag itself is not considered for the output. Unlike InnerText, though, InnerHtml lets you work with HTML rich text and doesn’t automatically encode and decode text. In other words, InnerText retrieves and sets the content of the tag as plain text, whereas InnerHtml retrieves and sets the same content but in HTML format.
HTML Input controls
In HTML, the <input> element has several variations and can be used to provide a submit button as well as a check box or text box. In ASP.NET, each possible instance of the <input> element is mapped to a specific class. All input classes derive from the HtmlInputControl class. HtmlInputControl is the abstract class that defines the common programming interface for all input controls. The class inherits from HtmlControl and simply adds three custom properties—Name, Type, and Value—to the inherited interface.
The Name property returns the name assigned to the control. In ASP.NET, this property is peculiar because, although it’s marked as read/write, it actually works as a read-only property. The get accessor returns the control’s UniqueID property, while the set accessor is just void. As a result, whatever value Web developer assigns to the property, either programmatically or declaratively, is just ignored and no exception or compile error is ever thrown.
The Type property mirrors the type attribute of the HTML input elements. The property is read-only. Finally, the Value property is read/write and represents the content of the input field.