Online Book Reader

Home Category

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

By Root 5628 0
in Figure 10-10, the control displays a pager with a few predefined links (first, previous, next, and last) and automatically selects the correct subset of rows that fit in the selected page.

Figure 10-10. Moving through pages in a GridView control.

The default user interface you get with the GridView doesn’t include the page number. Adding a page number label is as easy as writing a handler for the PageIndexChanged event:

protected void GridView1_PageIndexChanged(object sender, EventArgs e)

{

ShowPageIndex();

}

private void ShowPageIndex()

{

CurrentPage.Text = (GridView1.PageIndex + 1).ToString();

}

Once again, note that the PageIndexChanged handler is not involved with data binding or page selection as it is with DataGrids. If you don’t need any post-paging operation, you can blissfully omit it altogether.

What’s the cost of this apparently free (and magical) paging mechanism?

The GridView control doesn’t really know how to get a new page. It simply asks the bound data source control to return the rows that fit in the specified page. Paging is ultimately up to the data source control. When a grid is bound to a SqlDataSource control, paging requires that the whole data source be bound to the control. When a grid is bound to an ObjectDataSource control, paging depends on the capabilities of the business object you’re connecting to.

When the AllowPaging property is set to true, the grid displays a pager bar. You can control the characteristics of the pager to a large extent, through the and tags or their equivalent properties. The pager of the GridView control also supports first and last page buttons and lets you assign an image to each button. (This is also possible for DataGrids, but it requires a lot of code.) The pager can work in either of two modes—displaying explicit page numbers, or providing a relative navigation system. In the former case, the pager contains numeric links, one representing a page index. In the latter case, buttons are present to navigate to the next or previous page and even to the first or last page. The Mode property rules the user interface of the pager. Available modes are listed in Table 10-14.

Table 10-14. Modes of a Grid Pager

Mode

Description

NextPrevious

Displays next and previous buttons to access the next and previous pages of the grid

NextPreviousFirstLast

Displays next and previous buttons, plus first and last buttons to directly access the first and last pages of the grid

Numeric

Displays numeric link buttons corresponding to the pages of the grid

NumericFirstLast

Displays numeric link buttons corresponding to the pages of the grid, plus first and last buttons to directly access the first and last pages of the grid

Ad hoc pairs of properties—xxxPageText and xxxPageImageUrl—let you set the labels for these buttons as desired. The xxx stands for any of the following: First, Last, Next, or Previous.

Sorting Data


Sorting is a delicate, nonlinear operation that normally is quite expensive if performed on the client. Generally speaking, in fact, the best place to sort records is in the database environment because of the super-optimized code you end up running most of the time. Be aware of this as we examine the sorting infrastructure of the GridView control and data source controls. The GridView doesn’t implement a sorting algorithm; instead, it relies on the data source control (or the page, if bound to an enumerable object) to provide sorted data.

To enable the GridView’s sorting capabilities, you set the AllowSorting property to true. When sorting is enabled, the GridView gains the ability of rendering the header text of columns as links. You can associate each column with a sorting expression by using the SortExpression property. A sorting expression is any comma-separated sequence of column names. Each column name can be enriched with an order qualifier such as DESC or ASC. DESC indicates a descending order, while ASC denotes the ascending order. The ASC qualifier is the default; if the order qualifier

Return Main Page Previous Page Next Page

®Online Book Reader