日期:2008-11-02  浏览次数:20373 次

For More Information on ASP.NET
This article examines how to create a databound listbox using ASP.NET. For more information on ASP.NET be sure to check out the articles in the ASP.NET Article Index. The code in this article is based on the Beta 2 version of ASP.NET. To learn more about the free ASP.NET Beta 2, be sure to check out this FAQ.  


Introduction
One of the most common tasks developers are faced with when working with data-driven Web sites is the need to page data. Most data is only worthwhile if it can easily be digested by a human, so a data-driven Web site needs to present data in an easy-to-read format. In situations where a large chunk of data is presented to the user, it helps to break up this information into multiple pages.

Paging data is nothing new, just about every search engine and eCommerce site employs the technique. If you wonder over to Google and search on ASP you'll get back over five million results! Imagine if Google attempted to show all five million matches on one Web page! Instead, to make the information digestable by human eyes (i.e., yours), Google presents the results in chunks of ten records per page.

In this article we will look at how to implement paging database results using ASP.NET. It is surprisingly simple, requiring just a few lines of code!

Database Paging in Classic ASP
Paging in classic ASP was possible via a number of means. One of the most common ways was to use the paging properties provided in the ADO Recordset object. Even when using these properties, developers still were required to write a lot of code to handle paging correctly. (For more information on paging results using this method in classic ASP, be sure to read this article.) Other methods were available as well, such as using a stored procedure as well client-side script techniques. However, all of these methods required much code and, usually, a hapless intermixing of HTML and script code.

Database Paging in ASP.NET
Fortunately, paging database results in ASP.NET is much cleaner and much easier with ASP.NET than with classic ASP. In this article we'll look at using a DataGrid Web control to implement paging. The beautiful thing about the DataGrid Web control is that it handles paging itself, meaning the amount of actual code we have to write is very little indeed. First things first, though, let's look at an ASP.NET Web page that binds a DataSet to a DataGrid Web control. (We'll then examine how to page these results.)

Below you can see what the HTML portion of our ASP.NET page needs to look like. At absolute minimum, it just needs to contain a DataGrid control. I have set some properties in the DataGrid control to enhance its appearance; these ehancements, however, are optional:

<html>
<body>
    <asp:datagrid id="dgPopularFAQs" runat="server" BorderWidth="0"
       CellPadding="2" Width="100%"
       Font-Name="Verdana"
       Font-Size="Smaller"
       
       HeaderStyle-HorizontalAlign="Center"
       HeaderStyle-Font-Bold="True"
       HeaderStyle-BackColor="Navy"
       HeaderStyle-ForeColor="White"
                
       AlternatingItemStyle-BackColor="#dddddd">
    </asp:datagrid>
</body>
</html>




Next, we need to write code that will qu