国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Paging Database Results in ASP.NET (Prt2)(轉載:http:

2024-07-10 13:08:05
字體:
來源:轉載
供稿:網友
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, we'd like to page this data. in this part, we'll 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





that's 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 control's 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 control's currentpageindex property. also note that we'll 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>



that's all that's 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 datagrid's 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!





發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝兴县| 麻栗坡县| 利辛县| 信阳市| 怀化市| 潮安县| 德钦县| 阳江市| 赣榆县| 格尔木市| 云林县| 建湖县| 福鼎市| 竹北市| 长顺县| 五大连池市| 天津市| 巧家县| 孙吴县| 石棉县| 安新县| 栾川县| 务川| 呼和浩特市| 福海县| 内黄县| 安远县| 自治县| 肃北| 留坝县| 武威市| 江达县| 衡山县| 罗定市| 图木舒克市| 县级市| 德化县| 宜丰县| 兰考县| 仁化县| 兰考县|