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

来源:互联网 发布:计算机编程可以自学吗 编辑:程序博客网 时间:2024/05/25 05:34
<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="http://pagead2.googlesyndication.com/pagead/show_ads.js"></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="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
For More http://www.66of.com" target=_blank>information on http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET
This article examhttp://www.66of.com" target=_blank>ines how to create a databound listbox ushttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET. For more http://www.66of.com" target=_blank>information on http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET be sure to check out the articles http://www.66of.com" target=_blank>in the http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET Article http://www.66of.com" target=_blank>index. The code http://www.66of.com" target=_blank>in this article is based on the Beta 2 version of http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET. To learn more about the free http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET Beta 2, be sure to check out this FAQ.  


http://www.66of.com" target=_blank>introduction
One of the most common tasks developers are faced with when workhttp://www.66of.com" 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 http://www.66of.com" target=_blank>in an easy-to-read format. http://www.66of.com" target=_blank>in situations where a large chunk of data is presented to the user, it helps to break up this http://www.66of.com" target=_blank>information http://www.66of.com" target=_blank>into multiple pages.

http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing data is nothhttp://www.66of.com" target=_blank>ing new, just about every search enghttp://www.66of.com" target=_blank>ine and eCommerce site employs the technique. If you wonder over to Google and search on http://www.66of.com" target=_blank>ASP you'll get back over five million http://www.66of.com" target=_blank>Results! Imaghttp://www.66of.com" target=_blank>ine if Google attempted to show all five million matches on one Web page! http://www.66of.com" target=_blank>instead, to make the http://www.66of.com" target=_blank>information digestable by human eyes (i.e., yours), Google presents the http://www.66of.com" target=_blank>Results http://www.66of.com" target=_blank>in chunks of ten records per page.

http://www.66of.com" target=_blank>in this article we will look at how to implement http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>Results ushttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET. It is surprishttp://www.66of.com" target=_blank>ingly simple, requirhttp://www.66of.com" target=_blank>ing just a few lhttp://www.66of.com" target=_blank>ines of code!

http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>in Classic http://www.66of.com" target=_blank>ASP
http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>in classic http://www.66of.com" target=_blank>ASP was possible via a number of means. One of the most common ways was to use the http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing properties provided http://www.66of.com" target=_blank>in the ADO Recordset object. Even when ushttp://www.66of.com" target=_blank>ing these properties, developers still were required to write a lot of code to handle http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing correctly. (For more http://www.66of.com" target=_blank>information on http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>Results ushttp://www.66of.com" target=_blank>ing this method http://www.66of.com" target=_blank>in classic http://www.66of.com" target=_blank>ASP, be sure to read this article.) Other methods were available as well, such as ushttp://www.66of.com" target=_blank>ing a stored procedure as well client-side script techniques. However, all of these methods required much code and, usually, a hapless http://www.66of.com" target=_blank>intermixhttp://www.66of.com" target=_blank>ing of HTML and script code.

http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>in http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET
Fortunately, http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>Results http://www.66of.com" target=_blank>in http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET is much cleaner and much easier with http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET than with classic http://www.66of.com" target=_blank>ASP. http://www.66of.com" target=_blank>in this article we'll look at ushttp://www.66of.com" target=_blank>ing a DataGrid Web control to implement http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing. The beautiful thhttp://www.66of.com" target=_blank>ing about the DataGrid Web control is that it handles http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing itself, meanhttp://www.66of.com" target=_blank>ing the amount of actual code we have to write is very little http://www.66of.com" target=_blank>indeed. First thhttp://www.66of.com" target=_blank>ings first, though, let's look at an http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET Web page that bhttp://www.66of.com" target=_blank>inds a DataSet to a DataGrid Web control. (We'll then examhttp://www.66of.com" target=_blank>ine how to page these http://www.66of.com" target=_blank>Results.)

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

<html>
<body>
    <http://www.66of.com" target=_blank>ASP:datagrid id="dgPopularFAQs" runat="server" BorderWidth="0"
       CellPaddhttp://www.66of.com" target=_blank>ing="2" Width="100%"
       Font-Name="Verdana"
       Font-Size="Smaller"
       
       HeaderStyle-HorizontalAlign="Center"
       HeaderStyle-Font-Bold="True"
       HeaderStyle-BackColor="Navy"
       HeaderStyle-ForeColor="White"
                
       Alternathttp://www.66of.com" target=_blank>ingItemStyle-BackColor="#dddddd">
    </http://www.66of.com" target=_blank>ASP:datagrid>
</body>
</html>




Next, we need to write code that will query our http://www.66of.com" target=_blank>Database, populathttp://www.66of.com" target=_blank>ing a DataSet with the http://www.66of.com" target=_blank>Results of some SQL query. Once we have this populated DataSet, we'll bhttp://www.66of.com" target=_blank>ind the DataSet to the DataGrid Web control, dgPopularFAQs. The code for connecthttp://www.66of.com" target=_blank>ing to a Microsoft SQL Server 7.0 or greater http://www.66of.com" target=_blank>Database can be seen below. (If you are ushttp://www.66of.com" target=_blank>ing a different http://www.66of.com" target=_blank>Database, change all http://www.66of.com" 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)
    Bhttp://www.66of.com" target=_blank>indData()
  End Sub
    
    
  Sub Bhttp://www.66of.com" target=_blank>indData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(<i>ConnectionStrhttp://www.66of.com" target=_blank>ing</i>)

    '2. Create the command object, passhttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>in the SQL strhttp://www.66of.com" target=_blank>ing
    Const strSQL as Strhttp://www.66of.com" 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()
    myDA.Fill(myDS)

    '5. Set the datagrid's datasource to the dataset and databhttp://www.66of.com" target=_blank>ind
    dgPopularFAQs.DataSource = myDS
    dgPopularFAQs.DataBhttp://www.66of.com" target=_blank>ind()    
  End Sub
</script>

... HTML section omitted for brevity ...


[View a live demo!]

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


Connect to the http://www.66of.com" 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 http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>Results.

Populate the DataSet object with the http://www.66of.com" target=_blank>Results from the http://www.66of.com" target=_blank>Database query.

Bhttp://www.66of.com" target=_blank>ind the DataSet to the DataGrid Web control.
Note that with http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET it's that easy to display http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>information http://www.66of.com" target=_blank>in a visually appealhttp://www.66of.com" 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 mixhttp://www.66of.com" target=_blank>ing of HTML and script code, as would be the case http://www.66of.com" target=_blank>in a classic http://www.66of.com" target=_blank>ASP page.

http://www.66of.com" target=_blank>in Part 2 we'll look at how to improve the data display of the DataGrid Web control by havhttp://www.66of.com" target=_blank>ing the data paged. As you'll see http://www.66of.com" target=_blank>in Part 2, this only requires a few lhttp://www.66of.com" target=_blank>ines of code!


://www.dooba.cn/wangzhanyunyhttp://www.66of.com" target=_blank>ing/wangzhanyouhua/" title="seo,搜索引擎优化">seover="whttp://www.66of.com" target=_blank>indow.status='正文--http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>Results http://www.66of.com" target=_blank>in http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET (http://www.66of.com" target=_blank>Prt1)(http://www.66of.com" target=_blank>转载:http://www.4guysfromrolla.com/)[登级:初级]';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="http://pagead2.googlesyndication.com/pagead/show_ads.js"></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="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击