Online Book Reader

Home Category

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

By Root 5695 0
set to false, the [All] item will be cleared before data-bound items are added.

The DataKeyField Property


The DataKeyField property gets or sets the key field in the specified data source. The property serves the need of some data list controls that allow item selection and master/ detail views. Controls that support this property are viewable in Figure 10-1 and are DataGrid, DataList, and view controls.

All of these controls allow you to select a displayed item over a postback. Following the selection, however, these controls provide some reference about the data item associated with the selected row. The DataKeyField indicates which property on the bound data item identifies the selected record.

Note that the identification of the record is unequivocal only if the field is uniquely constrained in the original data source.

public virtual string DataKeyField {get; set;}

For example, imagine you display customers in a grid and allow users to click and drill down on the orders placed by that customer. When the user clicks, a postback occurs in which you can retrieve the value of the key field that uniquely identifies the selected data item. By setting DataKeyField to the CustomerId property—presumably the primary key field—you retrieve the ID of the selected customer and can plan further drill-down queries.

The DataKeyField property is coupled with the DataKeys array property. When DataKeyField is set, DataKeys contains the value of the specified key field for all the control’s data items currently displayed in the page. You retrieve the actual key value using the following expression:

// Gets you the value of the specified data key for the item at the given position

GridView1.DataKeys[GridView1.SelectedIndex];

Most controls, however, provide a handy SelectedValue property that just wraps the previous expression.

Note

View controls (for example, GridView and FormView) have a richer programming interface, and they extend the DataKeyField property to an array of strings and rename it to DataKeyNames. In this way, you can identify data items using multiple key values.

Data-Bound Controls


Data-bound controls are components whose whole interface is driven by one or more columns of data read from of a feasible data source. As you can see in Figure 10-1, there are quite a few types of data-bound controls. We can summarize that into three main categories: list controls, iterative controls and, the functionally richest of all, view controls.

List Controls


List controls display (or at least store in memory) many items at the same time—specifically, the contents of the bound data source. Depending on its expected behavior, the control picks the needed items from memory and properly formats and displays them. List controls include DropDownList, CheckBoxList, RadioButtonList, ListBox, and BulletedList. All list controls inherit from the base ListControl class. Let’s find out some more details.

The DropDownList Control


The DropDownList control enables users to select one item from a single-selection drop-down list. You can specify the size of the control by setting its height and width in pixels, but you can’t control the number of items displayed when the list drops down. Table 10-1 lists the most commonly used properties of the control.

Table 10-1. Properties of the DropDownList 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

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

Return Main Page Previous Page Next Page

®Online Book Reader