Basic Web Controls

Table 3.2 briefly describes several server controls that we have classified as basic Web controls. Some of these controls behave similarly. For example, the usages and characteristics of a CheckBoxList control are almost identical to those of a RadioButtonList control. This is why we have grouped these controls under single captions in Table 3.2.

Table 3.2 Basic Server Controls Server Control Characteristics

Label

TextBox

Buttons:

Button

LinkButton ImageButton

CheckBox

A Label is used to display text. If we want to display static text, we do not need a Label server control; we should instead use HTML. We should use a Label server control only if we need to change its properties via server code.

A TextBox control enables the user to enter text. By default, the TextMode property is SingleLine, but it can also be set to Multiline or Password. In case of Multiline text box, the Rows property determines the height. If its AutoPostBack property is set to True, it generates a PostBack on its Text_Changed() event.

All three types of buttons cause PostBacks when the user clicks them.

Button controls can be placed inside other container controls, such as DataList, DataGrid and Repeater. The LinkButton renders a hyperlink in the page. The ImageButton displays an image that responds to mouse clicks. We can also use it as an image map. Thus, we may pinpoint where in the graphic the user has clicked.

It enables the user to input Boolean data: true or false, yes or no. Its Checked property can also be bound to a data field of a data source. Its CheckedChanged event can be used for AutoPostBack.

These controls are derived from the ListControl abstract class. Note: these controls will be discussed in detail in a

ListControls:

■ CheckBoxList

■ DropDownList later section of this chapter.

■ RadioButtonList

Continued

Table 3.2 Continued

Server Control Characteristics

HyperLink It displays a link to another page. It is typically displayed as text specified in its Text property. It can also be displayed as an image specified in the ImageUrl property. If both the Text and ImageUrl properties are set, the ImageUrl property is displayed. If the image does not exist, then the text in the Text property is shown. Internet Explorer uses the Text property to display ToolTip.

Image We may use the Image control to display an image on the

Web page. The ImageUrl property specifies the path to the displayed image. When the image does not exist, we can specify the text to display in place of the image by setting the AlternateText property. The Image control only displays an image. If we need to capture mouse clicks on the image, we should instead use the ImageButton control.

Panel This can be used as a container of other controls. This control is rendered as an HTML <div> element.

RadioButton It creates an individual radio button on the page. We can group them to present mutually exclusive choices.

Table It enables us an HTML table. A table can be built at design time with static content, but the Table control is often built programmatically with dynamic contents. Programmatic additions or modifications to a table row or cell do not persist on PostBack. Changes to table rows or cells must be reconstructed after each post to the server. In these cases, better alternatives are DataList or DataGrid controls.

Xml This control can be used to transform XML documents.

Many of the basic server controls work very similarly to their HTML server control counterparts. All of the Web controls are prefixed with asp: in their tags. For example, the tag for a label Web control is <asp:Label>.Their uses are also mostly intuitive. All of the examples illustrated in the HTML server control section can also be effectively developed using Web controls. In this section we will present a number of additional examples to demonstrate the uses ofWeb controls.