Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [135]

By Root 5504 0
like a control. If a tag includes the runat=“server” attribute, ASP.NET creates an instance of a specific class; otherwise, if no runat attribute has been specified, the text is compiled into a LiteralControl object. Literal controls are simple text holders that are added to and removed from pages using the same programming interface defined for other server controls.

Note that a literal control is created for each sequence of characters placed between two successive server controls, including carriage returns. Using a new line to separate distinct server controls and increase code readability actually affects the number of server controls being created to serve the page. Writing the page as a single string without carriage returns produces the smallest number of server controls.

Web Controls


Web controls are defined in the System.Web.UI.WebControls namespace and represent an alternative approach to HTML server controls. Like HTML controls, Web controls are server-side components that spring to life thanks to the runat=“server” attribute. Unlike HTML controls, Web controls provide a programming interface that refactors the classic set of HTML attributes and events. For this reason, Web controls sometimes appear to be more consistent and abstract in the API design and richer in functionality, but they still generate valid markup. When hosted in .aspx pages, Web controls are characterized by the asp namespace prefix.

To a large degree, Web controls and HTML controls overlap and generate almost the same markup, although they do it through different programming interfaces. For example, the Web controls namespace defines the TextBox control and makes it available through the tag; similarly, the HTML controls namespace provides the HtmlInputText control and declares it using the tag. Using either is mostly a matter of preference; only in a few cases will you run into slight functionality differences.

Generalities of Web Controls


The WebControl class is the base class from which all Web controls inherit. WebControl inherits from Control. The class defines several properties and methods that are shared, but not necessarily implemented, by derived controls. Most properties and methods are related to the look and feel of the controls (font, style, colors, CSS) and are subject to browser and HTML versions. For example, although all Web controls provide the ability to define a border, not all underlying HTML tags actually support a border.

Properties of Web Controls


Table 6-9 lists the properties available on the WebControl class.

Table 6-9. Specific Properties of Web Controls

Property

Description

AccessKey

Gets or sets the letter to press (together with Alt) to quickly set focus to the control in a Web form. It’s supported on Internet Explorer 4.0 and newer.

Attributes

Gets the collection of attributes that do not correspond to properties on the control. Attributes set in this way will be rendered as HTML attributes in the resulting page.

BackColor

Gets or sets the background color of the Web control.

BorderColor

Gets or sets the border color of the Web control.

BorderStyle

Gets or sets the border style of the Web control.

BorderWidth

Gets or sets the border width of the Web control.

ControlStyle

Gets the style of the Web server control. The style is an object of type Style.

ControlStyleCreated

Gets a value that indicates whether a Style object has been created for the ControlStyle property.

CssClass

Get or sets the name of the cascading style sheet (CSS) class to be associated with the control.

DisabledCssClass

Get or sets the name of the cascading style sheet (CSS) class to be associated with the control when in a disabled state.

Enabled

Gets or sets whether the control is enabled.

Font

Gets the font properties associated with the Web control.

ForeColor

Gets or sets the foreground color of the Web control mostly used to draw text.

Height

Gets or sets the height of the control. The height is expressed as a member of type Unit.

Return Main Page Previous Page Next Page

®Online Book Reader