2018年3月2日 星期五

ASP.NET 使用DataTable實作分頁

參考資料:How To Copy DataRows Between DataTables by Using Visual Basic .NET

  這個作法是將資料庫的資料先存到DataTable,然後程式碼中會設定每一頁顯示幾筆資料,並算出目前從第幾筆資料到第幾筆資料,換頁之後在計算出,下一頁該從第幾筆到第幾筆資料,只是這個作法在每一次換頁的時候,都會重新和資料庫拿資料。

前端程式碼
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test_pagination.aspx.vb" Inherits="test_test_pagination" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link rel="stylesheet" href="../css/bootstrap.css" />
    <script src="../js/jquery-1.11.3.js"></script>
    <script src="../js/bootstrap.js"></script>
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <nav id="nav_pagehead" runat="server">
            <ul id="paging_head" runat="server" class="pagination">
            </ul>
        </nav>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <nav>
            <ul id="paging_foot" runat="server" class="pagination">
            </ul>
        </nav>
    </div>
    </form>
</body>
</html>

後端程式碼
Imports System.Data
Imports System.Data.SqlClient

Partial Class test_test_pagination
    Inherits System.Web.UI.Page
    Dim str_sql As String = String.Empty
    Dim def_dt As DataTable = Nothing

    '=============================
    Dim prows As Integer = 20 '每一頁多少筆數(此處100筆分成一頁)
    Dim this_page As Integer = 1 '目前在第幾頁
    Dim maxpage As Integer = 10 '最多顯示頁數(不變動)
    Dim showpage As Integer = 10 '計算目前顯示頁數

    Sub Call_get_paging()
        '取得目前分頁頁數
        If (Not Request("p") Is Nothing) Then
            If Regex.IsMatch(Request("p").ToString, "^\d+$") = False Then '數字會回傳true
                '不是數字,設定回1
                this_page = 1
            Else
                this_page = CInt(Request("p").ToString)
            End If
        Else
            this_page = 1
        End If
    End Sub

    Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
        If Not Page.IsPostBack Then


            def_dt = Nothing
            str_sql = "select TOP 5000 * from Student"
 '假設def_dt已經有資料了,請自行換成自己的DataTable
            def_dt = queryDataTable2(str_sql)

            Call_get_paging() '取得目前的頁數,傳到this_page變數

            Dim rows_count As Integer = 0 '資料的總筆數
            rows_count = def_dt.Rows.Count

            Dim dt1 As DataTable = CopyData_NewDataTable(def_dt, this_page, prows)
            If Not dt1 Is Nothing Then
                GridView1.DataSource = dt1
                GridView1.DataBind()
                GridView1.CssClass = "table table-bordered"
            End If
            def_dt.Clear()
            def_dt.Dispose()
            def_dt = Nothing
            'Response.Write("total_rows=" & total_rows & "<br/>")

            If rows_count > 0 Then
                's1開始頁數;tot_x1 全部的頁數
                Dim s1 As Integer = 1, tot_x1 As Integer = 0
                tot_x1 = Fix(rows_count / prows) '總筆數/每一頁多少筆數
                '總筆數 求餘數,餘數>0就加1頁
                If (CInt(rows_count) Mod prows) > 0 Then
                    tot_x1 = tot_x1 + 1
                End If
                '==========================
                '頁數變動
                If this_page >= 7 Then
                    If ((tot_x1 + 1) - ((this_page - 6) + 1)) >= maxpage Then
                        s1 = (this_page - 6) + 1
                        showpage = maxpage + (this_page - 6)
                    Else
                        s1 = ((this_page - 6) + 1) + (((tot_x1 + 1) - ((this_page - 6) + 1)) - maxpage)
                        showpage = tot_x1
                    End If
                Else
                    '預設
                    s1 = 1 '第一頁開始
                    showpage = maxpage '顯示的頁數
                End If
                '==========================
                If tot_x1 > 1 Then
                    Dim str_ago As String = String.Empty, str_back As String = String.Empty, str_num As String = String.Empty
                    Dim str_navpag As String = String.Empty, str_navpag_laquo As String = String.Empty, str_navpag_ago As String = String.Empty, str_navpag_back As String = String.Empty, str_navpag_raquo As String = String.Empty
                    For i As Integer = s1 To tot_x1
                        str_num = CStr(i)
                        If str_num.Length = 1 Then
                            str_num = "0" & str_num
                        End If
                        If i = this_page Then
                            str_navpag = str_navpag & "<li class=" & Chr(34) & "active" & Chr(34) & "><a>" & str_num & "<span class=" & Chr(34) & "sr-only" & Chr(34) & ">(current)</span></a></li>"
                            If i > 1 Then
                                str_navpag_ago = "<li><a href=" & Chr(34) & "test_pagination.aspx?p=" & (i - 1) & Chr(34) & " aria-label=" & Chr(34) & "Previous" & Chr(34) & "><span aria-hidden=" & Chr(34) & "true" & Chr(34) & ">&lt;</span></li>"
                                If i > 2 Then
                                    str_navpag_laquo = "<li><a href=" & Chr(34) & "test_pagination.aspx?p=1" & Chr(34) & " aria-label=" & Chr(34) & "Previous" & Chr(34) & "><span aria-hidden=" & Chr(34) & "true" & Chr(34) & ">&laquo;</span></li>"
                                End If
                            End If
                            If i < tot_x1 Then
                                str_navpag_back = "<li><a href=" & Chr(34) & "test_pagination.aspx?p=" & (i + 1) & Chr(34) & " aria-label=" & Chr(34) & "Next" & Chr(34) & "><span aria-hidden=" & Chr(34) & "true" & Chr(34) & ">&gt;</span></li>"
                                If (tot_x1 - i) > 2 Then
                                    str_navpag_raquo = "<li><a href=" & Chr(34) & "test_pagination.aspx?p=" & tot_x1 & Chr(34) & " aria-label=" & Chr(34) & "Next" & Chr(34) & "><span aria-hidden=" & Chr(34) & "true" & Chr(34) & ">&raquo;</span></li>"
                                End If
                            End If
                        Else
                            str_navpag = str_navpag & "<li><a href=" & Chr(34) & "test_pagination.aspx?p=" & i & Chr(34) & ">" & str_num & "</a></li>"
                        End If
                        If i = showpage Then
                            Exit For
                        End If
                    Next

                    Me.paging_head.InnerHtml = str_navpag_laquo & str_navpag_ago & str_navpag & str_navpag_back & str_navpag_raquo
                    Me.paging_foot.InnerHtml = str_navpag_laquo & str_navpag_ago & str_navpag & str_navpag_back & str_navpag_raquo
                Else
                    Me.nav_pagehead.Attributes.Add("Style", "display:none;")
                    Me.paging_head.InnerHtml = ""
                    Me.paging_foot.InnerHtml = ""
                End If
            Else
                Me.nav_pagehead.Attributes.Add("Style", "display:none;")
                Me.paging_head.InnerHtml = ""
                Me.paging_foot.InnerHtml = ""
            End If
        End If


    End Sub

    Function CopyData_NewDataTable(ByVal def_dt1 As DataTable, ByVal this_page As Integer, ByVal get_count As Integer) As DataTable
        Dim New_dt As DataTable = Nothing
        If Not def_dt1 Is Nothing Then
            '從第幾筆開始,第幾筆結束
            Dim data_Start_count As Integer = 0, data_End_count As Integer = 0
            data_Start_count = (this_page * get_count) - get_count + 1
            data_End_count = this_page * get_count

            New_dt = def_dt1.Clone() '將table的結構複製到新的DataTable
            For y1 As Integer = data_Start_count To data_End_count
                If (y1 - 1) > (def_dt1.Rows.Count - 1) Then
                    Exit For
                Else
                    New_dt.ImportRow(def_dt1.Rows(y1 - 1))
                End If
            Next
            Return New_dt
        Else
            Return Nothing
        End If

    End Function

End Class

沒有留言:

張貼留言