2015年7月28日 星期二

ASP.NET 手寫分頁

test_paging.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="test_paging.aspx.vb" Inherits="testdb_mssql.test_paging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="gv_p1" runat="server" style=" width:500px;">
        分頁顯示在此處
        </div>
    </div>
    </form>
</body>
</html>

test_paging.aspx.vb
Public Class test_paging
    Inherits System.Web.UI.Page
    Dim this_page As Integer = 1 '目前在第幾頁
    Dim maxpage As Integer = 10 '最多顯示頁數(不變動)
    Dim showpage As Integer = 10 '最多顯示頁數

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            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
            Dim rows_count As Integer = 2701 '假設此變數是資料的總筆數
            Dim str_temp1 As String = String.Empty
            's1開始頁數;tot_x1 全部的頁數
            Dim s1 As Integer = 1, tot_x1 As Integer = 0
            tot_x1 = Fix(rows_count / 100) '總筆數/每一頁多少筆數
            '總筆數 求餘數,餘數>0就加1頁
            If (CInt(rows_count) Mod 100) > 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
            '==========================
            Dim str_ago As String = String.Empty, str_back As String = String.Empty, str_num 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_temp1 = str_temp1 & "<a style=" & Chr(34) & "text-decoration:none" & Chr(34) & ">" & str_num & "</a>&nbsp;"
                    If i > 1 Then
                        str_ago = "<a href=" & Chr(34) & "test_paging.aspx?p=" & (i - 1) & Chr(34) & " style=" & Chr(34) & "text-decoration:none" & Chr(34) & ">&lt;</a>&nbsp;"
                    End If
                    If i < tot_x1 Then
                        str_back = "<a href=" & Chr(34) & "test_paging.aspx?p=" & (i + 1) & Chr(34) & " style=" & Chr(34) & "text-decoration:none" & Chr(34) & ">&gt;</a>&nbsp;"
                    End If
                Else
                    str_temp1 = str_temp1 & "<a href=" & Chr(34) & "test_paging.aspx?p=" & i & Chr(34) & ">" & str_num & "</a>&nbsp;"
                End If
                If i = showpage Then
                    Exit For
                End If
            Next
            Me.gv_p1.InnerHtml = str_ago & str_temp1 & str_back
        End If
    End Sub
End Class

沒有留言:

張貼留言