Online Book Reader

Home Category

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

By Root 5300 0
’s true by default.) This is the property you want to set to false to avoid the scenario in which when row 1 is selected on page 1 and you move to another page, row 1 is selected automatically also on the new page.

FooterRow

Returns a GridViewRow object that represents the footer.

HeaderRow

Returns a GridViewRow object that represents the header.

PageCount

Gets the number of pages required to display the records of the data source.

PageIndex

Gets and sets the 0-based index that identifies the currently displayed page of data.

PageSize

Indicates the number of records to display on a page.

Rows

Gets a collection of GridViewRow objects that represent the data rows currently displayed in the control.

SelectedDataKey

Returns the DataKey object for the currently selected record.

SelectedPersistedDataKey

Returns the DataKey object for the record selected on the previous page.

SelectedIndex

Gets and sets the 0-based index that identifies the row currently selected.

SelectedRow

Returns a GridViewRow object that represents the currently selected row.

SelectedValue

Returns the explicit value of the key as stored in the DataKey object. It’s similar to SelectedDataKey.

TopPagerRow

Returns a GridViewRow object that represents the top pager of the grid.

The GridView is designed to leverage the new data source object model, and it works best when bound to a data source control via the DataSourceID property. The GridView also supports the classic DataSource property, but if you bind data in that way, some of the features (such as built-in updates and paging) become unavailable.

Events of the GridView Control


Many controls in ASP.NET feature pairs of events of the type doing/done. Key operations in the control life cycle are wrapped by a pair of events—one firing before the operation takes place, and one firing immediately after the operation is completed. The GridView class is no exception. The list of events is shown in Table 10-10.

Table 10-10. Events Fired by the GridView Control

Event

Description

PageIndexChanging, PageIndexChanged

Both events occur when one of the pager buttons is clicked. They fire before and after the grid control handles the paging operation, respectively.

RowCancelingEdit

Occurs when the Cancel button of a row in edit mode is clicked, but before the row exits edit mode.

RowCommand

Occurs when a button is clicked.

RowCreated

Occurs when a row is created.

RowDataBound

Occurs when a data row is bound to data.

RowDeleting, RowDeleted

Both events occur when a row’s Delete button is clicked. They fire before and after the grid control deletes the row, respectively.

RowEditing

Occurs when a row’s Edit button is clicked, but before the control enters edit mode.

RowUpdating, RowUpdated

Both events occur when a row’s Update button is clicked. They fire before and after the grid control updates the row, respectively.

SelectedIndexChanging, SelectedIndexChanged

Both events occur when a row’s Select button is clicked. The two events occur before and after the grid control handles the select operation, respectively.

Sorting, Sorted

Both events occur when the hyperlink to sort a column is clicked. They fire before and after the grid control handles the sort operation, respectively.

RowCreated and RowDataBound events are the same as the DataGrid’s ItemCreated and ItemDataBound events, with new names. The same is true of the RowCommand event, which is the same as the DataGrid’s ItemCommand event.

The availability of events that announce a certain operation significantly enhances your programming power. By hooking the RowUpdating event, you can cross-check what is being updated and validate the new values. Likewise, you might want to handle the RowUpdating event to HTML-encode the values supplied by the client before they are persisted to the underlying data store. This simple trick helps you to fend off script injections.

Binding Data to the Grid


If no data source property is set, the GridView control doesn’t render

Return Main Page Previous Page Next Page

®Online Book Reader