首页 > 编程 > .NET > 正文

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

2024-07-10 13:04:44
字体:
来源:转载
供稿:网友
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 query our database, populating a dataset with the results of some sql query. once we have this populated dataset, we'll bind the dataset to the datagrid web control, dgpopularfaqs. the code for connecting to a microsoft sql server 7.0 or greater database can be seen below. (if you are using a different database, change all 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)
    binddata()
  end sub
    
    
  sub binddata()
    '1. create a connection
    dim myconnection as new sqlconnection(<i>connectionstring</i>)

    '2. create the command object, passing in the sql string
    const strsql as string = "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 databind
    dgpopularfaqs.datasource = myds
    dgpopularfaqs.databind()    
  end sub
</script>

... html section omitted for brevity ...


[view a live demo!]

note that the connectionstring property should be a valid connection string for the sql database. for example: server=ipaddress;uid=username;pwd=password;database=databasename. (of course, the oledb connection string is slightly different; refer to the documentation for more information.) note that we are performing five steps here:


connect to the 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 database results.

populate the dataset object with the results from the database query.

bind the dataset to the datagrid web control.
note that with asp.net it's that easy to display database information in a visually appealing 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 mixing of html and script code, as would be the case in a classic asp page.

in part 2 we'll look at how to improve the data display of the datagrid web control by having the data paged. as you'll see in part 2, this only requires a few lines of code!



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表