Enabling Paging

IOTA^_^    

ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 16.  Using the DataGrid Control


The Sales By Category view in the Northwind sample database returns over 80 rows of data far too many to display on one page. When you initially set up the DataGrid control, you changed the AllowPaging property to True. This causes, by default, 10 rows of data to be sent out for each page. At the bottom of the grid you see paging links (by default, < and >). Once you've added a little code, you can use these links to page through the data, 10 rows at a time. You can also change the number of rows per page by setting the PageSize property of the DataGrid control. In addition, you can change the style of the paging indicators (adding page numbers rather than simple Next and Previous links). If you want to allow users to page using Next and Previous links, you can also use your own images in place of the text links.

Basically, there are three types of paging mechanisms you can use to help your users browse through the data:

  • Next and Previous links (the default behavior)

  • Numeric links, displaying page number links

  • A custom pager, where you take complete control over how the paging links appear to the user

In order to use any of these techniques, you'll need to add a bit of code. In the control's PageIndexChanged event, you must do the following:

  • Indicate to the control what its new CurrentPage property should be

  • Rebind the control to its data source

The first requirement is easy: The PageIndexChanged event procedure passes to you an object whose NewPageIndex property indicates the value you need to place in the CurrentPage property of the grid. The second part is simple, too. You've already added the GridLoad procedure to the page. Now you simply need to call that procedure again.

To add support for paging to your DataGrid control, follow these steps:

  1. With CategorySales.aspx open in the page designer, select the View, Code menu item to view the page's code-behind file.

  2. At the top of the code editor window, from the drop-down list on the left, select grdCatSales.

  3. From the drop-down list on the right, select PageIndexChanged. This inserts the appropriate event-handling procedure in the class for you.

  4. Modify the grdCatSales_PageIndexChanged procedure so that it looks like this:

     Private Sub grdCatSales_PageIndexChanged( _  ByVal source As System.Object, _  ByVal e As System.Web.UI.WebControls. _  DataGridPageChangedEventArgs) _  Handles grdCatSales.PageIndexChanged   grdCatSales.CurrentPageIndex = e.NewPageIndex   GridLoad() End Sub 

Note that the second parameter, e, is a DataGridPageChangedEventArgs object. This object provides the NewPageIndex property, and setting the grid's CurrentPageIndex property to e.NewPageIndex causes the DataGrid control to only display data from the selected page the next time it redraws. (The grid's PageSize property, 10 by default, controls the number of rows displayed per page. Given the page number and the page size, the grid can retrieve exactly the rows it needs from its data source and display just those rows.)

graphics/tryitout.gif

With this code in place, you should now be able to browse to the page and select the < and > links to display subsets of the data. Follow these steps to try out the paging capability you've added to the DataGrid control:

  1. In the Solution Explorer, right-click CategorySales.aspx and select Build and Browse from the context menu.

  2. Use the links at the bottom of the grid to page through the data. By clicking the right angle bracket, you can move forward through the pages. By clicking the left angle bracket, you can move backward through the pages.

TIP

By default, the Pager hyperlink appears at the bottom of the DataGrid control. By modifying the Position property, you can set the pager to the top instead or both the top and bottom.


Customizing the Pager

If you want to modify the "look" of your pager, you can use the Properties dialog box, as you saw earlier. Imagine that you'd like to display the following:

  • Numeric paging

  • Ten page numbers

  • Numbers at bottom of the grid

  • Numbers centered

To add these effects, follow these steps:

  1. With CategorySales.aspx open in the page designer, right-click the DataGrid control.

  2. Select Property Builder from the context menu.

  3. Select the Paging tab and then modify the properties as shown in Table 16.6.

    Table 16.6. Set These Paging Properties
    Property Value
    Position Bottom
    Mode Page numbers
    Numeric buttons 10

  4. Select the Format tab, select Pager from the Objects list, and set the Horizontal Alignment property to Center.

  5. Click OK to dismiss the dialog box.

graphics/tryitout.gif

After making the changes outlined in the previous section, browse to the page and verify that you see the paging indicators, as shown in Figure 16.10.

Figure 16.10. You can format the paging indicators to match your own needs.

graphics/16fig10.jpg


    IOTA^_^    
    Top


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net