Paging Database Results in ASP.NET (Prt1)(转载:http:

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src=""> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src=""> </script>
For More" target=_blank>information on" target=_blank>ASP." target=_blank>NET
This article exam" target=_blank>ines how to create a databound listbox us" target=_blank>ing" target=_blank>ASP." target=_blank>NET. For more" target=_blank>information on" target=_blank>ASP." target=_blank>NET be sure to check out the articles" target=_blank>in the" target=_blank>ASP." target=_blank>NET Article" target=_blank>index. The code" target=_blank>in this article is based on the Beta 2 version of" target=_blank>ASP." target=_blank>NET. To learn more about the free" target=_blank>ASP." target=_blank>NET Beta 2, be sure to check out this FAQ." target=_blank>introduction
One of the most common tasks developers are faced with when work" target=_blank>ing 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" target=_blank>in an easy-to-read format." target=_blank>in situations where a large chunk of data is presented to the user, it helps to break up this" target=_blank>information" target=_blank>into multiple pages." target=_blank>Pag" target=_blank>ing data is noth" target=_blank>ing new, just about every search eng" target=_blank>ine and eCommerce site employs the technique. If you wonder over to Google and search on" target=_blank>ASP you'll get back over five million" target=_blank>Results! Imag" target=_blank>ine if Google attempted to show all five million matches on one Web page!" target=_blank>instead, to make the" target=_blank>information digestable by human eyes (i.e., yours), Google presents the" target=_blank>Results" target=_blank>in chunks of ten records per page." target=_blank>in this article we will look at how to implement" target=_blank>Pag" target=_blank>ing" target=_blank>Database" target=_blank>Results us" target=_blank>ing" target=_blank>ASP." target=_blank>NET. It is surpris" target=_blank>ingly simple, requir" target=_blank>ing just a few l" target=_blank>ines of code!" target=_blank>Database" target=_blank>Pag" target=_blank>ing" target=_blank>in Classic" target=_blank>ASP" target=_blank>Pag" target=_blank>ing" target=_blank>in classic" target=_blank>ASP was possible via a number of means. One of the most common ways was to use the" target=_blank>Pag" target=_blank>ing properties provided" target=_blank>in the ADO Recordset object. Even when us" target=_blank>ing these properties, developers still were required to write a lot of code to handle" target=_blank>Pag" target=_blank>ing correctly. (For more" target=_blank>information on" target=_blank>Pag" target=_blank>ing" target=_blank>Results us" target=_blank>ing this method" target=_blank>in classic" target=_blank>ASP, be sure to read this article.) Other methods were available as well, such as us" target=_blank>ing a stored procedure as well client-side script techniques. However, all of these methods required much code and, usually, a hapless" target=_blank>intermix" target=_blank>ing of HTML and script code." target=_blank>Database" target=_blank>Pag" target=_blank>ing" target=_blank>in" target=_blank>ASP." target=_blank>NET
Fortunately," target=_blank>Pag" target=_blank>ing" target=_blank>Database" target=_blank>Results" target=_blank>in" target=_blank>ASP." target=_blank>NET is much cleaner and much easier with" target=_blank>ASP." target=_blank>NET than with classic" target=_blank>ASP." target=_blank>in this article we'll look at us" target=_blank>ing a DataGrid Web control to implement" target=_blank>Pag" target=_blank>ing. The beautiful th" target=_blank>ing about the DataGrid Web control is that it handles" target=_blank>Pag" target=_blank>ing itself, mean" target=_blank>ing the amount of actual code we have to write is very little" target=_blank>indeed. First th" target=_blank>ings first, though, let's look at an" target=_blank>ASP." target=_blank>NET Web page that b" target=_blank>inds a DataSet to a DataGrid Web control. (We'll then exam" target=_blank>ine how to page these" target=_blank>Results.)

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

    <" target=_blank>ASP:datagrid id="dgPopularFAQs" runat="server" BorderWidth="0"
       CellPadd" target=_blank>ing="2" Width="100%"
       Alternat" target=_blank>ingItemStyle-BackColor="#dddddd">
    </" target=_blank>ASP:datagrid>

Next, we need to write code that will query our" target=_blank>Database, populat" target=_blank>ing a DataSet with the" target=_blank>Results of some SQL query. Once we have this populated DataSet, we'll b" target=_blank>ind the DataSet to the DataGrid Web control, dgPopularFAQs. The code for connect" target=_blank>ing to a Microsoft SQL Server 7.0 or greater" target=_blank>Database can be seen below. (If you are us" target=_blank>ing a different" target=_blank>Database, change all" target=_blank>instances of Sql below to OleDb (i.e., change Dim myDA as New SqlDataAdapter() to Dim myDA as New OleDbDataAdapter()).)

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    B" target=_blank>indData()
  End Sub
  Sub B" target=_blank>indData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(<i>ConnectionStr" target=_blank>ing</i>)

    '2. Create the command object, pass" target=_blank>ing" target=_blank>in the SQL str" target=_blank>ing
    Const strSQL as Str" target=_blank>ing = "SELECT FAQID, Description, DateEntered, ViewCount " & _
                             "FROM tblFAQ ORDER BY FAQID"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    '3. Create the DataAdapter
    Dim myDA as New SqlDataAdapter()
    myDA.SelectCommand = myCommand

    '4. Populate the DataSet
    Dim myDS as New DataSet()

    '5. Set the datagrid's datasource to the dataset and datab" target=_blank>ind
    dgPopularFAQs.DataSource = myDS
    dgPopularFAQs.DataB" target=_blank>ind()    
  End Sub

... HTML section omitted for brevity ...

[View a live demo!]

Note that the ConnectionStr" target=_blank>ing property should be a valid connection str" target=_blank>ing for the SQL" target=_blank>Database. For example: server=IPAddress;uid=userName;pwd=password;" target=_blank>Database=" target=_blank>DatabaseName. (Of course, the OleDb connection str" target=_blank>ing is slightly different; refer to the documentation for more" target=_blank>information.) Note that we are perform" target=_blank>ing five steps here:

Connect to the" target=_blank>Database.

Create the command object based upon the SQL query we wish to run and the connection object we wish to run it on.

Create the DataAdapter. This object is needed to fill a DataSet with the" target=_blank>Database" target=_blank>Results.

Populate the DataSet object with the" target=_blank>Results from the" target=_blank>Database query.

B" target=_blank>ind the DataSet to the DataGrid Web control.
Note that with" target=_blank>ASP." target=_blank>NET it's that easy to display" target=_blank>Database" target=_blank>information" target=_blank>in a visually appeal" target=_blank>ing format. Do take a moment to view the live demo and note how nice the DataGrid control appears. Also note that with this technique there is no shameless mix" target=_blank>ing of HTML and script code, as would be the case" target=_blank>in a classic" target=_blank>ASP page." target=_blank>in Part 2 we'll look at how to improve the data display of the DataGrid Web control by hav" target=_blank>ing the data paged. As you'll see" target=_blank>in Part 2, this only requires a few l" target=_blank>ines of code!

11AIXW35011944.jpg" target=_blank>ing/wangzhanyouhua/" title="seo,搜索引擎优化">seover="w" target=_blank>indow.status='正文--" target=_blank>Pag" target=_blank>ing" target=_blank>Database" target=_blank>Results" target=_blank>in" target=_blank>ASP." target=_blank>NET (" target=_blank>Prt1)(" target=_blank>转载:[登级:初级]';return true">
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src=""> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src=""> </script>
个人分类: .net
想对作者说点什么? 我来说一句

asp.net2.0 大数据量分页

2008年08月28日 1.03MB 下载


2009年12月01日 2.43MB 下载