Paging Database Results in asp.net (Prt2) (reproduced: http://www.4guysfromrolla.com/) [Boarding: primary]

Source: Internet
Author: User
Tags bind classic asp
asp.net paging Database Results in asp.net
by Scott Mitchell


--------------------------------------------------------------------------------


Read Part 1


--------------------------------------------------------------------------------

In the Part 1 we looked at the I-bind a DataSet to the DataGrid Web control. However, in our Live demo We noted this sheer number of results made the data hard to consume for visitors. Ideally, we like to page this data. In this part, we'll look at the implement paging using the DataGrid Web control. It is surprisingly easy!

Database paging with a DataGrid Web control
To implement paging and 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-a Page-level event handler that contains the Follo Wing Definition:
Sub eventhandlername (sender as Object, E as DataGridPageChangedEventArgs)
...
End Sub





That's all there are to it! So, in order to make the DataGrid we examined in Part 1 able to page data, we must the needed properties and Eve NT 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 This I also took a moment to set the PageSize property to a value of 15. This property indicates how many records to show the per page; If not specified, it defaults to a value of 10. Now all we need to does is provide the event handler as a page index is changed, dgpopularfaqs_paged. The code for this event handler are painfully simple:all we need to do are set the DataGrid Web control ' 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" >

... Functions/subs omitted for brevity ...

Sub dgpopularfaqs_paged (sender as Object, E as DataGridPageChangedEventArgs)
Dgpopularfaqs.currentpageindex = E.newpageindex
Binddata ()
End Sub
</script>



Note so we need to recreate and rebind we datasets to the DataGrid Web control after we set the DataGrid Web Control ' C Urrentpageindex property. Also Note "We'll want to" the Page_Load event handler so this binddata () subroutine is only called when the The page is a visited. On any postbacks, the Dgpopularfaqs_paged event handler'll handle recreating the DataSet and rebinding it to the Datagri D Web Control. To implement 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

... Functions/subs omitted for brevity ...
</script>



That's all of that ' s needed! A Live demo of the paged DataGrid Web control can is seen; Note This in the live demo some further enhancements are made to the DataGrid Web control in order to improve the end OUTP Ut. Additionally, the DataGrid employs a PagerStyle tag, which provides for a is more customized output of the paging controls.

Caveats
The most important thing to realize when paging data and the DataGrid ' s AllowPaging property is this time the user n Avigates to a new page of the entire DataSet is rebuilt. While this isn't a big concern for small datasets, imagine so are wanting to page the results of a SQL query that R Esults in, say, 1,000 rows. Such an approach would is taxing, since the database would have to rebuild a 1,000-row datasets each and every R visited any page of the data. The DataGrid Web control provides a allowcustompaging option that does does not suffer from this limitation. To learn more about custom paging, consult the documentation.

Also Note this to utilize the default paging and a DataGrid Web control for your use a DataSet. This is, if your set AllowPaging to True but don't employ custom paging, you cannot bind a SqlDataReader or Oledbdatareade R to the DataGrid Web control and implement paging. If you would receive an error along the lines of: ' To support paging, you must use ' attempt ' supp Orts the ICollection interface.

This error message occurs because the DataReader objects does not support the ICollection. Note that the DataSet does not support this intercace directly either; However, the DataSet does support the IListSource interface, which is inherited to the IList interface, which is inherit Ed 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 the database data with asp.net are painfully easy. By using the DataGrid Web control and just a few lines's code, you can simply implement a nice-looking, Easy-to-use G System. Compare this technique and output to the mountains of classic ASP script code is required. Bleh. For more information on ASP.net is sure to check out the ASP.net Article Index.

Happy programming!




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.