您的位置:首页 > 理论基础 > 计算机网络

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

2008-05-01 02:53 597 查看
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
By Scott Mitchell

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

Read Part 1

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

http://www.66of.com" target=_blank>in Part 1 we looked at how to bhttp://www.66of.com" target=_blank>ind a DataSet to the DataGrid Web control. However, http://www.66of.com" target=_blank>in our live demo we noted that the sheer number of http://www.66of.com" target=_blank>Results made the data hard to consume for visitors. Ideally, we'd like to page this data. http://www.66of.com" target=_blank>in this part, we'll look at how to implement http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing ushttp://www.66of.com" target=_blank>ing the DataGrid Web control. It is surprishttp://www.66of.com" target=_blank>ingly easy!

http://www.66of.com" target=_blank>Database http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing with a DataGrid Web Control
To implement http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing with the DataGrid Web control, perform the followhttp://www.66of.com" target=_blank>ing simple steps:

Set the Allowhttp://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing property of the DataGrid Web control to True.

Set the OnPagehttp://www.66of.com" target=_blank>indexChanged event handler of the DataGrid Web control to a Page-level event handler that contahttp://www.66of.com" target=_blank>ins the followhttp://www.66of.com" target=_blank>ing defhttp://www.66of.com" target=_blank>inition:
Sub EventHandlerName(sender as Object, e as DataGridPageChangedEventArgs)
...
End Sub

That's all there is to it! So, http://www.66of.com" target=_blank>in order to make the DataGrid we examhttp://www.66of.com" target=_blank>ined http://www.66of.com" target=_blank>in Part 1 able to page data, we must first set the needed properties and event handlers of the DataGrid Web control. The followhttp://www.66of.com" target=_blank>ing HTML content illustrates these changes:

<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"
AutoGenerateColumns="False"

HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Font-Bold="True"
HeaderStyle-BackColor="Navy"
HeaderStyle-ForeColor="White"

Alternathttp://www.66of.com" target=_blank>ingItemStyle-BackColor="#dddddd"

Allowhttp://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing="True"
PageSize="15"
OnPagehttp://www.66of.com" target=_blank>indexChanged="dgPopularFAQs_Paged">
...
</http://www.66of.com" target=_blank>ASP:datagrid>

Note that I also took a moment to set the PageSize property to a value of 15. This property http://www.66of.com" target=_blank>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 http://www.66of.com" target=_blank>index is changed, dgPopularFAQs_Paged. The code for this event handler is pahttp://www.66of.com" target=_blank>infully simple: all we need to do is set the DataGrid Web control's CurrentPagehttp://www.66of.com" target=_blank>index property to the value of the new page, which is passed http://www.66of.com" target=_blank>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.CurrentPagehttp://www.66of.com" target=_blank>index = e.NewPagehttp://www.66of.com" target=_blank>index
Bhttp://www.66of.com" target=_blank>indData()
End Sub
</script>

Note that we need to recreate and rebhttp://www.66of.com" target=_blank>ind our DataSet to the DataGrid Web control after we set the DataGrid Web control's CurrentPagehttp://www.66of.com" target=_blank>index property. Also note that we'll want to change the Page_Load event handler so that the Bhttp://www.66of.com" target=_blank>indData() subrouthttp://www.66of.com" target=_blank>ine is only called when the page is first visited. On any postbacks, the dgPopularFAQs_Paged event handler will handle recreathttp://www.66of.com" target=_blank>ing the DataSet and rebhttp://www.66of.com" target=_blank>indhttp://www.66of.com" target=_blank>ing it to the DataGrid Web control. To implement this change http://www.66of.com" target=_blank>in the Page_Load event handler, use the followhttp://www.66of.com" target=_blank>ing code:

<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
Bhttp://www.66of.com" target=_blank>indData()
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 http://www.66of.com" target=_blank>in the live demo some further enhancements are made to the DataGrid Web control http://www.66of.com" target=_blank>in order to improve the end output. Additionally, the DataGrid employs a PagerStyle tag, which provides for a more customized output of the http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing controls.

Caveats
The most important thhttp://www.66of.com" target=_blank>ing to realize when http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing data with the DataGrid's Allowhttp://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing 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, imaghttp://www.66of.com" target=_blank>ine that you are wanthttp://www.66of.com" target=_blank>ing to page the http://www.66of.com" target=_blank>Results of a SQL query that http://www.66of.com" target=_blank>Results http://www.66of.com" target=_blank>in, say, 1,000 rows. Such an approach would be taxhttp://www.66of.com" target=_blank>ing, shttp://www.66of.com" target=_blank>ince the http://www.66of.com" target=_blank>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 AllowCustomhttp://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing option that does not suffer from this limitation. To learn more about custom http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing, consult the documentation.

Also note that to utilize the default http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing with a DataGrid Web control you must use a DataSet. That is, if you set Allowhttp://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing to True but do not employ custom http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing, you cannot bhttp://www.66of.com" target=_blank>ind a SqlDataReader or OleDbDataReader to the DataGrid Web control and implement http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing. If you attempt to do so you will receive an error along the lhttp://www.66of.com" target=_blank>ines of: "To support http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing, you must use an object that supports the ICollection http://www.66of.com" target=_blank>interface."

This error message occurs because the DataReader objects do not support the ICollection http://www.66of.com" target=_blank>interface. Note that the DataSet does not support this http://www.66of.com" target=_blank>intercace directly either; however, the DataSet does support the IListSource http://www.66of.com" target=_blank>interface, which is http://www.66of.com" target=_blank>inherited from the IList http://www.66of.com" target=_blank>interface, which is http://www.66of.com" target=_blank>inherited from the ICollection http://www.66of.com" target=_blank>interface. Hence, the DataSet can be used to implement http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing with the DataGrid Web control.

Conclusion
As this article has (hopefully) illustrated, http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing http://www.66of.com" target=_blank>Database data with http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET is pahttp://www.66of.com" target=_blank>infully easy. By ushttp://www.66of.com" target=_blank>ing the DataGrid Web control and just a few lhttp://www.66of.com" target=_blank>ines of code, you can simply implement a nice-lookhttp://www.66of.com" target=_blank>ing, easy-to-use http://www.66of.com" target=_blank>Paghttp://www.66of.com" target=_blank>ing system. Compare this technique and output to the mountahttp://www.66of.com" target=_blank>ins of classic http://www.66of.com" target=_blank>ASP script code that was required. Bleh. 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 http://www.66of.com" target=_blank>ASP.http://www.66of.com" target=_blank>NET Article http://www.66of.com" target=_blank>index.

Happy Programmhttp://www.66of.com" target=_blank>ing!



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>Prt2)(http://www.66of.com" target=_blank>转载:http://www.4guysfromrolla.com/)[登级:初级]';return true">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: