首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET GridView 分頁完整程式碼總結

     編輯:關於ASP.NET

提供一份可直接複製套用的 GridView 自訂分頁程式碼,包含前端分頁列(首頁、上頁、下頁、末頁、頁面大小下拉)與後端繫結邏輯。

前端標記 (.aspx)

<asp:Label ID="tplb" runat="server" Text="總頁數:"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>&nbsp;&nbsp;
<asp:Label ID="curLabel" runat="server" Text="當前頁:"></asp:Label>
<asp:Label ID="lblPage" runat="server" Text="1"></asp:Label>&nbsp;
<asp:LinkButton ID="lblFirstButton" runat="server" OnClick="lblFirstButton_Click">|&lt;</asp:LinkButton>&nbsp;
<asp:LinkButton ID="lblPreButton" runat="server" OnClick="lblPreButton_Click">&lt;</asp:LinkButton>&nbsp;
<asp:LinkButton ID="lblNextButton" runat="server" OnClick="lblNextButton_Click">&gt;</asp:LinkButton>&nbsp;
<asp:LinkButton ID="lblLastButton" runat="server" OnClick="lblLastButton_Click">&gt;|</asp:LinkButton>&nbsp;
<asp:DropDownList ID="ddlPage" runat="server" Width="40px" AutoPostBack="True"
    OnSelectedIndexChanged="ddlPage_SelectedIndexChanged">
    <asp:ListItem>10</asp:ListItem>
    <asp:ListItem>15</asp:ListItem>
    <asp:ListItem>20</asp:ListItem>
    <asp:ListItem>30</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="PageSizeLabel" runat="server" Text="條/頁"></asp:Label>

<asp:GridView ID="FollowExamInfoGridView" runat="server" AllowPaging="false" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="ExamDate" HeaderText="隨訪日期" />
        <asp:BoundField DataField="DoctorName" HeaderText="醫師" />
        <asp:BoundField DataField="Result" HeaderText="結果" />
    </Columns>
</asp:GridView>

後端程式碼 (.aspx.cs)

#region 分頁邏輯
protected void BindFollowExamInfoGridView(int PersonID)
{
    int currentpage = Convert.ToInt32(lblPage.Text);
    DataTable dt = feibf.GetByPersonIDFollowExamInfo(PersonID); // 取得完整資料

    if (dt.Rows.Count > 0)
    {
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dt.DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = Convert.ToInt32(ddlPage.SelectedValue);
        lblPageCount.Text = ps.PageCount.ToString();

        ps.CurrentPageIndex = currentpage - 1;
        FollowExamInfoGridView.DataSource = ps;
        FollowExamInfoGridView.DataBind();

        // 按鈕啟用狀態
        lblPreButton.Enabled = (currentpage > 1);
        lblFirstButton.Enabled = (currentpage > 1);
        lblNextButton.Enabled = (currentpage < ps.PageCount);
        lblLastButton.Enabled = (currentpage < ps.PageCount);
    }
    else
    {
        FollowExamInfoGridView.DataSource = null;
        FollowExamInfoGridView.DataBind();
        lblPageCount.Text = "0";
        lblPreButton.Enabled = lblNextButton.Enabled = lblFirstButton.Enabled = lblLastButton.Enabled = false;
    }
}

protected void lblPreButton_Click(object sender, EventArgs e)
{
    lblPage.Text = (Convert.ToInt32(lblPage.Text) - 1).ToString();
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblNextButton_Click(object sender, EventArgs e)
{
    lblPage.Text = (Convert.ToInt32(lblPage.Text) + 1).ToString();
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblFirstButton_Click(object sender, EventArgs e)
{
    lblPage.Text = "1";
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void lblLastButton_Click(object sender, EventArgs e)
{
    lblPage.Text = lblPageCount.Text;
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
{
    lblPage.Text = "1";
    BindFollowExamInfoGridView(Convert.ToInt32(Request.QueryString["PersonID"]));
}
#endregion

適用場景與改進建議

  • 適用於資料量 < 5 萬筆、可一次載入記憶體的情境。
  • 大量資料請改用 ObjectDataSource + 自訂分頁存儲程序(參考「ASP.NET 大量資料分頁最佳實務」)。
  • 可將分頁邏輯封裝為 UserControl 或 BasePage 繼承,避免重複代碼。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved