提供一份可直接複製套用的 GridView 自訂分頁程式碼,包含前端分頁列(首頁、上頁、下頁、末頁、頁面大小下拉)與後端繫結邏輯。
<asp:Label ID="tplb" runat="server" Text="總頁數:"></asp:Label>
<asp:Label ID="lblPageCount" runat="server" Text=""></asp:Label>
<asp:Label ID="curLabel" runat="server" Text="當前頁:"></asp:Label>
<asp:Label ID="lblPage" runat="server" Text="1"></asp:Label>
<asp:LinkButton ID="lblFirstButton" runat="server" OnClick="lblFirstButton_Click">|<</asp:LinkButton>
<asp:LinkButton ID="lblPreButton" runat="server" OnClick="lblPreButton_Click"><</asp:LinkButton>
<asp:LinkButton ID="lblNextButton" runat="server" OnClick="lblNextButton_Click">></asp:LinkButton>
<asp:LinkButton ID="lblLastButton" runat="server" OnClick="lblLastButton_Click">>|</asp:LinkButton>
<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>
#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
ObjectDataSource + 自訂分頁存儲程序(參考「ASP.NET 大量資料分頁最佳實務」)。UserControl 或 BasePage 繼承,避免重複代碼。