貼)使用PagedDataSource給Repeater、DataList增加分頁

來源:互聯網
上載者:User
使用PagedDataSource給Repeater、DataList增加分頁

Repeater和DataList是資料庫中常用的資料顯示控制項,它們和DataGrid相比較因為沒有分頁等複雜的功能,因此效能高,同時Repeater自訂模板又給我們帶來了很大的靈活性。但是Repeater和DataList沒有分頁功能,有時很不方便。目前有很多增加分頁的方法,包括使用預存程序來控制每頁的資料讀取,這些分頁製作起來都很麻煩,下面介紹一種使用PagedDataSource給Repeater、DataList增加分頁的方法。

PagedDataSource類封裝了DataGrid控制項的屬性,從而使DataGrid控制項可以執行分頁,它就是一個資料的容器,我們先把資料從資料庫中讀取出來放在這個容器中,然後設定容器的屬性取出當前要顯示的頁上的部分資料,然後將此部分資料再綁定到頁面上的顯示控制項上。我們同樣可以將它用在Repeater和DataList中,下面給出一個執行個體協助大家理解如何使用PagedDataSource類。
頁面顯示元素如下:

後台代碼如下:

 1Private Sub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 2        '建立資料庫連接,擷取資料
 3        Dim sqlconn As New SqlConnection(ConfigurationSettings.AppSettings.Get("sqlserver"))
 4        sqlconn.Open()
 5        Dim sqlda As New SqlDataAdapter("select * from vote_items", sqlconn)
 6        Dim dt As New DataTable
 7        sqlda.Fill(dt)
 8        sqlda.Dispose()
 9
10        '將資料設定為PagedDataSource的資料來源,並設定相關參數
11        Dim pds As New PagedDataSource
12        '注意和其它控制項DataSource不同的是,這裡不能直接寫dt,因為PagedDataSource.DataSource實現了IEnumerable
13        '介面,而DataTable沒有實現此介面,只有DataView實現了此介面
14        pds.DataSource = dt.DefaultView
15        '==================================================
16        pds.AllowPaging = True
17        pds.PageSize = 3 '每頁顯示的項目數
18
19        '從URL參數中擷取page頁碼參數
20        Dim currentpage As Integer
21        If Request.QueryString.Get("page") <> Nothing Then
22            currentpage = Convert.ToInt32(Request.QueryString("Page"))
23        Else
24            currentpage = 1
25        End If
26        '設定當前要顯示的資料頁
27        pds.CurrentPageIndex = currentpage - 1
28
29        Label1.Text = "共有記錄" & pds.DataSourceCount & "條,分" & pds.PageCount & "頁顯示,每頁顯示" & _
30        pds.PageSize & "條,當前為第" & currentpage & "頁:"
31
32        '根據首頁和尾頁控制連結的顯示
33        If Not pds.IsFirstPage Then
34            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath & "?Page=" & Convert.ToInt32(currentpage - 1)
35            lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath & "?Page=1"
36        End If
37
38        If Not pds.IsLastPage Then
39            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath & "?Page=" & Convert.ToInt32(currentpage + 1)
40            lnkLast.NavigateUrl = Request.CurrentExecutionFilePath & "?Page=" & pds.PageCount
41        End If
42
43        '將PagedDataSource綁定到Repeater
44        rp.DataSource = pds
45        rp.DataBind()
46    End Sub

事實上,功能還有很多可以改進的地方,例如將使用連結控制頁數換成使用按鈕來控制,這裡就不多說了,大家可以自己研究一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.