Online Book Reader

Home Category

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

By Root 5772 0
the value of a list item

Items

Gets the collection of items in the list control

SelectedIndex

Gets or sets the index of the selected item in the list

SelectedItem

Gets the selected item in the list

SelectedValue

Gets the value of the selected item in the list

The DropDownList control, as well as many other server controls, features some properties to configure the graphical aspect of the final markup. At rendering time, these properties are transformed in cascading style sheet (CSS) style properties. The best practice today is to avoid style properties such as BorderColor and ForeColor and use CSS classes instead. Whenever possible and suitable, you should adhere to this de facto standard and emit plain HTML markup out of server controls.

The DataTextField and DataValueField properties don’t accept expressions, only plain property names. If you need to combine and display two or more fields from the data source, it is recommended that you preprocess that data at the source and bind data already in a display format.

Note

The ASP.NET DropDownList control doesn’t support groups of options as provided by the HTML element. There are various ways to work around this limitation.

To start off, you can create your own customized drop-down control and override the RenderContents methods. The method is invoked just when the control is requested to write out its markup. You can add a new attribute to any option that indicates the group. If you take this route, remember also to update the view state to also store the additional group attribute. I’ll return to custom controls and their view state management in the next chapter.

Another approach entails creating a < tagMapping> section in the configuration file and mapping standard DropDownList controls to your customized drop-down control. In this way, you don’t even need to change the markup of your ASPX pages and can just add option groups.

Finally, you can keep on using standard DropDownList controls but add some jQuery code that adds option groups on the fly as the page is loaded in the browser. I’ll cover jQuery in Chapter 21.

The CheckBoxList Control


The CheckBoxList control is a single monolithic control that groups a collection of selectable list items with an associated check box, each of which is rendered through an individual CheckBox control. The properties of the child check boxes are set by reading the associated data source. You insert a check box list in a page as follows:

Table 10-2 lists the specific properties of the CheckBoxList control.

Table 10-2. Properties of the CheckBoxList Control

Property

Description

AppendDataBoundItems

Indicates whether statically defined items should be maintained or cleared when adding data-bound items

AutoPostBack

Indicates whether the control should automatically post back to the server when the user changes the selection

CellPadding

Indicates pixels between the border and contents of the cell

CellSpacing

Indicates pixels between cells

DataMember

The name of the table in the DataSource to bind

DataSource

The data source that populates the items of the list

DataSourceID

ID of the data source component to provide data

DataTextField

Name of the data source field to supply the text of list items

DataTextFormatString

Formatting string used to visually format list items to be displayed

DataValueField

Name of the data source field used to supply the value of a list item

Items

Gets the collection of items in the list control

RepeatColumns

Gets or sets the number of columns to display in the control

RepeatDirection

Gets or sets a value that indicates whether the control displays vertically or horizontally

RepeatLayout

Gets or sets the layout of the check boxes: Table, Flow, OrderedList, UnorderedList

SelectedIndex

Gets or sets the index of the first selected item in the list—the one with the lowest index

SelectedItem

Gets the first selected item

SelectedValue

Gets the value

Return Main Page Previous Page Next Page

®Online Book Reader