如何使用PagedDataSource來做DataRepeater的分頁的效果

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
ASP.NET中的DataList和DataRepeater提供了簡單快速的方法來展現資料,
其<ItemTemplate>更是讓我們能隨心所欲的決定資料的排放方式.可惜的是他
們不像Datagrid那樣,有內建的分頁功能.

如何解決這個不能分頁的問題呢?在這篇文章中將會介紹如何使用 PagedDataSource Class 來解決分頁的問題.

簡單的介紹幾個常用到的 PagedDataSource Class Public Properties

DataSource - 資料來源
AllowPaging - true 允許分頁;false 不允許.
PageSize - 決定重複多少次
PageCount - 總共有幾頁
CurrentPageIndex - 目前所在的頁數
IsFirstPage - 是第一頁嗎,回傳True or False
IsLastPage - 是最後一頁嗎,回傳True or False

下面就是範例(剪貼就可以直接跑了)

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

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)

Dim Pgds As PagedDataSource = New PagedDataSource()
Pgds.DataSource = CreateDataSource().DefaultView
Pgds.AllowPaging = True
Pgds.PageSize = 6 '決定重複多少次
lblTotalPage.text = Pgds.PageCount.ToString()

Dim CurrentPage As Integer
If Not Request.QueryString("Page") Is Nothing Then
CurrentPage=Convert.ToInt32(Request.QueryString("Page"))
Else
CurrentPage=1
End If

Pgds.CurrentPageIndex = CurrentPage-1
lblCurrentPage.Text = "Page: " + CurrentPage.ToString()

If Not Pgds.IsFirstPage Then
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurrentPage-1)
End If

If Not Pgds.IsLastPage Then
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurrentPage+1)
End If

Repeater1.DataSource=Pgds
Repeater1.DataBind()

End Sub

Function CreateDataSource() As Datatable

'This part is an example from Asp.net QuickStart
'You can change this part with your own Dataset

Dim dt As DataTable
Dim dr As DataRow
Dim i As Integer

'create a DataTable
dt = New DataTable
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("DateTimeValue", GetType(DateTime)))
dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))

'Make some rows and put some sample data in
For i = 0 To 50
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = DateTime.Now.ToShortTimeString
If (i Mod 2 <> 0) Then
dr(3) = True
Else
dr(3) = False
End If
'add the row to the datatable
dt.Rows.Add(dr)
Next

Return dt

End Function

</script>

<html><head>
<title>DataRepeater Paging Example</title>
<style type=text/css>

BODY {
FONT: 10px Verdana, Arial, Helvetica, "sans serif"; COLOR: #000000;
}
.txt {
FONT-SIZE: 12px
}
</style>
</head>
<body>
<form name=form1 method=post runat="server">
<table class=txt width="100%" border=0>
<tbody><tr><td>

<asp:HyperLink id=lnkPrev runat="server"><< Prev</asp:HyperLink>
<asp:HyperLink id=lnkNext runat="server">Next >></asp:HyperLink>
<asp:Label id=lblCurrentPage runat="server"></asp:Label>
of <asp:Label id=lblTotalPage runat="server"></asp:Label>

</td></tr></tbody></table>
<asp:repeater id=Repeater1 runat="server">


<ItemTemplate>
<hr align="left" width="60%" size="1">
<table class=txt width="100%" border="0">
<tr>
<td>
Order Date: <%# DataBinder.Eval(Container.DataItem, "DateTimeValue", "{0:d}") %>
</td><tr><td>
Quantity: <%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:N2}") %>
</td><tr><td>
Item: <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
</td><tr><td>
Order Date: <asp:CheckBox id=chk1 Checked='<%# DataBinder.Eval(Container.DataItem, "BoolValue") %>' runat="server"/>
</td></tr></table>
</ItemTemplate>

</asp:repeater>
<hr hight="1">

<p>Datasource:Microsoft QuickStart Example

http://cht.gotdotnet.com/quickstart/aspplus/doc/webdatabinding.aspx
</p>
<p>Reference:MSDN .NET Framework Class Library

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolspageddatasourceclasstopic.asp
</p><p>
Paging with Repeater control in ASP.NET

http://www.charon.co.uk/content.aspx?CategoryID=28&ArticleID=21 </p></form></body></htm
11AIc59D0291E.jpg
seover="window.status='正文--如何使用PagedDataSource來做DataRepeater的分頁的效果';return true">
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: .net
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭