Programming Microsoft ASP.NET 4 - Dino Esposito [231]
More precisely, GetDataItem is the endpoint of a stack-based mechanism that traces the current binding context for the page. Each control in the control tree is pushed onto this stack at the time the respective DataBind method is called. When the DataBind method returns, the control is popped from the stack. If the stack is empty and you attempt to call Eval programmatically, GetDataItem throws an invalid operation exception. In summary, you can use the Eval shortcut only in templates; if you need to access properties of a data item anywhere else in the code, resort to DataBinder.Eval and indicate the data item object explicitly.
Managing Tables of Data
Let’s delve deeper into the programming features of a very popular and widely used view control—the GridView control.
The GridView’s Object Model
The GridView control provides a tabular, grid-like view of the contents of a data source. Each column represents a data source field, and each row represents a record. The GridView supports a large set of properties that fall into the following broad categories: behavior, visual settings, style, state, and templates. Table 10-5 details the properties that affect the behavior of the GridView.
Table 10-5. Behavior Properties of the GridView Control
Property
Description
AllowPaging
Indicates whether the control supports paging.
AllowSorting
Indicates whether the control supports sorting.
AutoGenerateColumns
Indicates whether columns are automatically created for each field in the data source. The default is true.
AutoGenerateDeleteButton
Indicates whether the control includes a button column to let users delete the record that is mapped to the clicked row.
AutoGenerateEditButton
Indicates whether the control includes a button column to let users edit the record that is mapped to the clicked row.
AutoGenerateSelectButton
Indicates whether the control includes a button column to let users select the record that is mapped to the clicked row.
ClientIDMode
Indicates the algorithm used to generate the client ID.
ClientIDRowSuffix
Gets and sets the names of the data fields whose values will be appended to the client ID when the client ID mode is set to Predictable.
ClientIDRowSuffixDataKeys
Gets and sets the values appended to the client ID.
DataMember
Indicates the specific table in a multimember data source to bind to the grid. The property works in conjunction with DataSource. If DataSource is a DataSet object, it contains the name of the particular table to bind.
DataSource
Gets or sets the data source object that contains the values to populate the control.
DataSourceID
Indicates the bound data source control.
RowHeaderColumn
Name of the column to use as the column header. This property is designed for improving accessibility.
SortDirection
Gets the direction of the column’s current sort.
SortExpression
Gets the current sort expression.
The SortDirection and SortExpression properties specify the direction and the sort expression on the column that currently determine the order of the rows. Both properties are set by the control’s built-in sorting mechanism when users click a column’s header. The whole sorting engine is enabled and disabled through the AllowSorting property.
Each row displayed within a GridView control corresponds to a special type of grid item. The list of predefined types of items is nearly identical to that of the DataGrid, and it includes items such as the header, rows and alternating rows, the footer, and the pager. These items are static in the sense that they remain in place for the lifetime of the control in the application. Other types of items are active for a short period of time—the time needed to accomplish a certain operation. Dynamic items are the edit row, selected row, and EmptyData item. EmptyData identifies the body of the grid when the grid is bound to an empty