源代碼部分(.aspx.vb)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在這裡放置使用者程式碼以初始化網頁
If Not IsPostBack Then
pds.DataSource = Me.CreateDataSource
pds.AllowPaging = True
pds.PageSize = 4
intTotalRec = Me.CreateDataSource.Count
pds.CurrentPageIndex = 0
'Response.Write("
")
intTotalPage = pds.PageCount.ToString
Me.DataList1.DataSource = pds
Me.DataList1.DataBind()
Me.showinfo()
'Session("pc") = "1"
Else
'含有Session的注釋代碼表示在頁面驅動了一個事件後
'會先執行page_load事件然後執行所對應的的事件.
'Session("pc") = Session("pc") + "2"
'Response.Write("
")
pds.DataSource = Me.CreateDataSource
pds.AllowPaging = True
pds.PageSize = 4
intTotalRec = Me.CreateDataSource.Count
'Response.Write("
")
intTotalPage = pds.PageCount.ToString
End If
End Sub
Public Function CreateDataSource() As DataView
Dim dt As DataTable
Dim dr As DataRow
dt = New DataTable
dt.Columns.Add("Integer", GetType(Integer))
dt.Columns.Add("String", GetType(String))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow
dr.Item(0) = i
dr.Item(1) = "item " & i.ToString
dt.Rows.Add(dr)
Next
CreateDataSource = dt.DefaultView
End Function
Private Sub lbFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbFirst.Click
'Session("pc") = Session("pc") + "3"
'Response.Write("
")
pds.CurrentPageIndex = 0
Me.DataList1.DataSource = pds
Me.DataList1.DataBind()
Me.showinfo()
End Sub
Private Sub lbLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbLast.Click
pds.CurrentPageIndex = Me.intTotalPage - 1
Me.DataList1.DataSource = pds
Me.DataList1.DataBind()
Me.showinfo()
End Sub
Private Sub lbPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbPrevious.Click
If pds.CurrentPageIndex = 0 Then
Else
pds.CurrentPageIndex -= 1
End If
Me.DataList1.DataSource = pds
Me.DataList1.DataBind()
Me.showinfo()
End Sub
Private Sub lbNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbNext.Click
If pds.CurrentPageIndex = pds.PageCount - 1 Then
Else
pds.CurrentPageIndex += 1
End If
Me.DataList1.DataSource = pds
Me.DataList1.DataBind()
Me.showinfo()
End Sub
Private Sub showinfo()
Me.lInfo.Text = (pds.CurrentPageIndex + 1).ToString & "/" & Me.intTotalPage.ToString
End Sub
Note:
需要給PagedDataSource賦一個數據源,同時還要設定允許分頁(AllowPaging)、每頁的大小(PageSize),這樣PagedDataSource對象就可以自動算出頁數(PageCount),然後就可以把這個PagedDataSource作為數據源賦於一個DataList對象,最後將DataList對象綁定。
本文地址:http://com.8s8s.com/it/it44202.htm