欢迎光临
我们一直在努力

建站超值云服务器,限时71元/月

paging database results in asp.net
by scott mitchell

——————————————————————————–

read part 1

——————————————————————————–

in part 1 we looked at how to bind a dataset to the datagrid web control. however, in our live demo we noted that the sheer number of results made the data hard to consume for visitors. ideally, wed like to page this data. in this part, well look at how to implement paging using the datagrid web control. it is surprisingly easy!

database paging with a datagrid web control
to implement paging with the datagrid web control, perform the following simple steps:

set the allowpaging property of the datagrid web control to true.

set the onpageindexchanged event handler of the datagrid web control to a page-level event handler that contains the following definition:
sub eventhandlername(sender as object, e as datagridpagechangedeventargs)
   …
end sub

thats all there is to it! so, in order to make the datagrid we examined in part 1 able to page data, we must first set the needed properties and event handlers of the datagrid web control. the following html content illustrates these changes:

<asp:datagrid id="dgpopularfaqs" runat="server" borderwidth="0"
              cellpadding="2" width="100%"
              font-name="verdana"
              font-size="smaller"
              autogeneratecolumns="false"
                
              headerstyle-horizontalalign="center"
              headerstyle-font-bold="true"
              headerstyle-backcolor="navy"
              headerstyle-forecolor="white"
                
              alternatingitemstyle-backcolor="#dddddd"
                
              allowpaging="true"
              pagesize="15"
              onpageindexchanged="dgpopularfaqs_paged">
   …
</asp:datagrid>        

note that i also took a moment to set the pagesize property to a value of 15. this property indicates how many records to show per page; if not specified, it defaults to a value of 10. now all we need to do is provide the event handler for when a page index is changed, dgpopularfaqs_paged. the code for this event handler is painfully simple: all we need to do is set the datagrid web controls currentpageindex property to the value of the new page, which is passed in through the datagridpagechangedeventargs parameter of the event handler.

<script language="vb" runat="server">

  … other functions/subs omitted for brevity …

  sub dgpopularfaqs_paged(sender as object , e as datagridpagechangedeventargs)
    dgpopularfaqs.currentpageindex = e.newpageindex
    binddata()
  end sub
</script>

note that we need to recreate and rebind our dataset to the datagrid web control after we set the datagrid web controls currentpageindex property. also note that well want to change the page_load event handler so that the binddata() subroutine is only called when the page is first visited. on any postbacks, the dgpopularfaqs_paged event handler will handle recreating the dataset and rebinding it to the datagrid web control. to implement this change in the page_load event handler, use the following code:

<script language="vb" runat="server">
  sub page_load(sender as object, e as eventargs)
    if not page.ispostback then
      binddata()
    end if    
  end sub

  … other functions/subs omitted for brevity …
</script>

thats all thats needed! a live demo of the paged datagrid web control can be seen; note that in the live demo some further enhancements are made to the datagrid web control in order to improve the end output. additionally, the datagrid employs a pagerstyle tag, which provides for a more customized output of the paging controls.

caveats
the most important thing to realize when paging data with the datagrids allowpaging property is that each time the user navigates to a new page the entire dataset is rebuilt. while this is not a big concern for small datasets, imagine that you are wanting to page the results of a sql query that results in, say, 1,000 rows. such an approach would be taxing, since the database would have to rebuild a 1,000-row dataset each and every time the user visited any page of the data. the datagrid web control provides an allowcustompaging option that does not suffer from this limitation. to learn more about custom paging, consult the documentation.

also note that to utilize the default paging with a datagrid web control you must use a dataset. that is, if you set allowpaging to true but do not employ custom paging, you cannot bind a sqldatareader or oledbdatareader to the datagrid web control and implement paging. if you attempt to do so you will receive an error along the lines of: "to support paging, you must use an object that supports the icollection interface."

this error message occurs because the datareader objects do not support the icollection interface. note that the dataset does not support this intercace directly either; however, the dataset does support the ilistsource interface, which is inherited from the ilist interface, which is inherited from the icollection interface. hence, the dataset can be used to implement paging with the datagrid web control.

conclusion
as this article has (hopefully) illustrated, paging database data with asp.net is painfully easy. by using the datagrid web control and just a few lines of code, you can simply implement a nice-looking, easy-to-use paging system. compare this technique and output to the mountains of classic asp script code that was required. bleh. for more information on asp.net be sure to check out the asp.net article index.

happy programming!

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 »
分享到: 更多 (0)