Home > Articles > Programming > ASP .NET

  • Print
  • + Share This
This chapter is from the book

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 class—specifically 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.1—just 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 row—can 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.3Figure 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 bottom—1, 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 bottom—2, 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.4Figure 8.4 The first page of data is displayed.


Figure 8.5Figure 8.5. By clicking on the ellipsis hyperlink, the fourth page of data is

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020