- Adding Paging Support to the DataGrid
- Providing a More Elegant Paging Interface
- Paging Through the DataGrid Using Custom Paging
- Paging Through a Cached DataSet
- Summary
- On the Web
Providing a More Elegant Paging Interface
The paging interface of Figures 8.1 and 8.2 was a simple pair of < and > hyperlinks at the bottom of the DataGrid. This interface has a set of stylistic properties much like the DataGrid's. For example, the paging interface has properties like BackColor, BorderStyle, Font, ForeColor, HorizontalAlign, Width, and others. The DataGrid's PagerStyle property can be used to set visual properties for the pager interface. Like the ItemStyle, HeaderStyle, and other "style" properties, the PagerStyle property is an instance of a classspecifically the DataGridPagerStyle class. The DataGridPagerStyle class has a number of properties that can be set, such as BackColor, Font, and Width. As with the DataGrid's other "style" properties, you set the PagerStyle instance's properties using the following notation:
<asp:DataGrid runat="server" id="MyDataGrid" PagerStyle-BackColor="Blue" PagerStyle-Font-Name="Verdana" ... />
Alternatively, you can set PagerStyle properties using the following notation:
<asp:DataGrid runat="server" id="MyDataGrid" ...> <PagerStyle BackColor="Blue" Font-Name="Verdana" /> ... </asp:DataGrid>
Table 8.1 gives a quick rundown of the more useful properties of the DataGridPagerStyle class. Not all properties of the DataGridPagerStyle class are displayed in Table 8.1just the more commonly used ones, including the ones we'll be examining in this chapter. For the full list of the DataGridPagerStyle class properties, be sure to refer to the technical documentation provided in the "On the Web" section at the end of this chapter.
Table 8.1 The DataGridPagerStyle Properties
Property |
Description |
BackColor |
Specifies the background color of the pager row. |
Font |
Specifies the font's name, size, and style options (bolded, italicized, underlined, and so on). |
ForeColor |
The foreground color of the pager row. |
HorizontalAlign |
The horizontal alignment of the pager rowcan be Left, Right, or Center. |
Mode |
Specifies whether the pager should be displayed as a Next/Previous set of hyperlinks or a list of links for each page. The default is to display Next/Previous hyperlinks. |
NextPageText |
The text to display for the Next hyperlink when Mode is set to display Next/Previous hyperlinks. The default is >. |
PrevPageText |
The text to display for the Next hyperlink when Mode is set to display Next/Previous hyperlinks. The default is <. |
PageButtonCount |
If Mode is set to display a list of links for each page, specifies how many pages of links to display. Defaults to 10. If this property is set to a value less than the total number of pages, after the last page link, an ellipsis (...) is displayed. |
Position |
Specifies where the paging hyperlinks should appear. Options include Bottom, Top, and TopAndBottom. |
Width |
The width of the pager row. Can be in pixels or percentages. |
Displaying the Paging Interface as Next/Previous Hyperlinks
The < and > hyperlinks we saw in Figure 8.1 and 8.2 can easily be changed to Next and Prev hyperlinks by simply setting the PagerStyle's NextPageText and PrevPageText to "Next" and "Prev", respectively. We can right-align or center-align these hyperlinks by setting the PagerStyle's HorizontalAlign property to Right or Center, respectively. In addition, we can specify the pager's font, background color, and foreground color by setting the Font, BackColor, and ForeColor properties.
Listing 8.3 provides an alternative pager interface from Listing 8.2. It utilizes the exact same server-side Visual Basic .NET code from Listing 8.2; the only changes are in the DataGrid's declaration.
Listing 8.3 The Pager Interface Is Highly Customizable
1: The server-side code block has been omitted for brevity. 2: Refer back to Listing 8.2 and 8.1 for its details. 3: 4: <form runat="server"> 5: <asp:DataGrid runat="server" id="dgTitles" 6: Font-Name="Verdana" Font-Size="9pt" CellPadding="5" 7: AlternatingItemStyle-BackColor="#dddddd" 8: AutoGenerateColumns="True" 9: PageSize="5" AllowPaging="True" 10: OnPageIndexChanged="dgTitles_Paging"> 11: 12: <HeaderStyle BackColor="Navy" ForeColor="White" Font-Size="13pt" 13: Font-Bold="True" HorizontalAlign="Center" /> 14: 15: <PagerStyle BackColor="Navy" ForeColor="White" Font-Size="8pt" 16: Font-Bold="True" HorizontalAlign="Right" 17: NextPageText="Next >" PrevPageText="< Prev" 18: Position="TopAndBottom" /> 19: </asp:DataGrid> 20: </form>
The PagerStyle properties in Listing 8.3 are set on lines 15 through 18. The BackColor and ForeColor are set to Navy and White (line 15), to match the colors of the DataGrid's header. The paging hyperlinks are right-aligned (line 16), and the next and previous hyperlinks are denoted by "Next >" and "< Prev" (line 17).
In addition, the Position property is set to TopAndBottom (line 18). The property specifies where the pager interface should appear on the DataGrid. Its default is Bottom, which places the pager interface after the DataGrid's Footer row; if the DataGrid's ShowFooter property is False (the default), the pager interface appears after the last Item row. If the Position property is set to Top, the pager interface is placed before the Header row; if the DataGrid's ShowHeader property is set to False, the pager interface appears after the last Item row. Finally, if the Position property is set to TopAndBottom, as in Listing 8.3, the pager interface is displayed both above and below the DataGrid's Header and Footer rows.
Figure 8.3 illustrates the placement of the pager interface when the Position property is set to TopAndBottom.
Figure 8.3. The pager interface is displayed both above and below the DataGrid Header and Footer rows.Displaying the Paging Interface as Hyperlinks for Each Page
The Next/Previous hyperlink interface is a good paging interface to use when the user is presented with a relatively small amount of data that he will likely want to step through in sequential order. However, if your DataGrid will contain tens or hundreds of pages, or if you expect your users will want to step through the pages in a random order, you might want to consider using an alternative paging interface.
By setting the PagerStyle's Mode property to NumericPages, the paging interface changes from a Next/Previous hyperlink model to one that contains a number of hyperlinks for the various pages. For example, if you have four pages in your DataGrid and set the Mode property to NumericPages in place of the Next/Previous hyperlinks, the user will see four numbers: 1, 2, 3, and 4. Three of these numbers will be hyperlinks that display the specific page; of the four pages, the one currently being visited is displayed as text, not as a hyperlink.
You might be wondering what will be displayed if the DataGrid has, say, 50 pages. Will there be a hyperlink for each of the 50 pages? To control how many hyperlinks will be displayed, you can set the PagerStyle's PageButtonCount property, which has a default value of 10. If you set the PageButtonCount property to 10 for a DataGrid that has 50 pages, initially only hyperlinks 1 through 10 will be displayed. After the 10 will be a hyperlinked ellipsis (...). If you click on, say, the 5 hyperlink, the fifth page of data will be displayed, along with the 1 to 10 hyperlinks at the bottom. If you click on the ellipsis hyperlink, page 10 will be shown. Additionally, the labels at the bottom will change to list links 10 to 20, with ellipses both before 10 and after 20.
The preceding description of how the PagerStyle's PageButtonCount property works might be a bit confusing. A sample should clear things up. Listing 8.4 contains a DataGrid declaration that sets the PagerStyle's Mode property to NumericPages. Because the data we are displaying is broken up into four pages, the PageButtonCount property has been set to 3 to illustrate the functionality of the NumericPages hyperlink interface.
Listing 8.4 The NumericPages Mode Displays Three Hyperlinks at the Bottom of Each Page
1: <form runat="server"> 2: <asp:DataGrid runat="server" id="dgTitles" 3: Font-Name="Verdana" Font-Size="9pt" CellPadding="5" 4: AlternatingItemStyle-BackColor="#dddddd" 5: AutoGenerateColumns="True" 6: PageSize="5" AllowPaging="True" 7: OnPageIndexChanged="dgTitles_Paging"> 8: 9: <HeaderStyle BackColor="Navy" ForeColor="White" Font-Size="13pt" 10: Font-Bold="True" HorizontalAlign="Center" /> 11: 12: <PagerStyle BackColor="Navy" ForeColor="White" Font-Size="8pt" 13: Font-Bold="True" HorizontalAlign="Right" 14: Mode="NumericPages" PageButtonCount="3" /> 15: </asp:DataGrid> 16: </form>
The pager interface is set on lines 12 through 14 in Listing 8.4. The important properties to focus on are Mode and PageButtonCount on line 14. The Mode property specifies that we are to use a NumericPages hyperlink interface, while the PageButtonCount indicates that three hyperlinks should be displayed per page.
When the code in Listing 8.4 is visited in a browser for the first time, the DataGrid's first page of data is displayed as shown in Figure 8.4. The pager interface lists the first three pages at the bottom1, 2, and 3, with 2 and 3 as hyperlinks, and 1 as text (because we're currently on the first page). Additionally, after the 3 hyperlink, an ellipsis hyperlink is displayed.
If the ellipsis hyperlink is clicked, page 4 of the DataGrid's data is displayed. The pager interface lists three pages at the bottom2, 3, and 4, with 2 and 3 as hyperlinks, and 4 as text (as we're currently on the fourth page). Additionally, before the 2 hyperlink is an ellipsis hyperlink that takes the user back to the first page.
Figure 8.4 illustrates the DataGrid displaying the first page of data, as seen when the page is first visited. Figure 8.5 shows the output if the user clicks the ellipsis hyperlink from Figure 8.4. In Listing 8.4, the fourth page of data is being displayed, and the pager interface contains references to pages 2, 3, and 4, with an ellipsis before the 2 hyperlink.
Figure 8.4 The first page of data is displayed.
Figure 8.5. By clicking on the ellipsis hyperlink, the fourth page of data is