AspNetPager 是 ASP.NET WebForms 時代最流行的分頁控件,官網 webdiyer.com 提供文檔與存儲過程生成工具。
CREATE PROC Paged
@pageIndex INT,
@pageCount INT OUTPUT,
@pageSize INT
AS
DECLARE @count INT
SELECT @count = COUNT(*) FROM dbo.Student
SET @pageCount = CEILING(@count * 1.0 / @pageSize)
SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY dbo.Student.stuId) AS tempId, *
FROM dbo.Student
) AS stu
WHERE tempId >= @pageSize * (@pageIndex - 1) + 1
AND tempId <= @pageIndex * @pageSize
使用 ROW_NUMBER() 窗口函數實現高效分頁,適用 SQL Server 2005+。
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
CustomInfoHTML="共%PageCount%頁,當前第%CurrentPageIndex%頁,每頁%PageSize%條,共%RecordCount%條"
FirstPageText="首頁" LastPageText="尾頁" NextPageText="下一頁" PrevPageText="上一頁"
PageIndexBoxType="TextBox" ShowCustomInfoSection="Left" ShowPageIndex="False"
ShowPageIndexBox="Always" SubmitButtonText="Go" TextAfterPageIndexBox="頁"
TextBeforePageIndexBox="轉到" OnPageChanging="AspNetPager1_PageChanging"
AlwaysShow="True" PageSize="10" ShowMoreButtons="false" HorizontalAlign="Center">
</webdiyer:AspNetPager>
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) InitGridView();
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) {
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
InitGridView();
}
private void InitGridView() {
int count, pageCount;
gridStudent.DataSource = new BLL.StudentBLL().GetStudents(
AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out pageCount, out count);
gridStudent.DataBind();
AspNetPager1.RecordCount = count; // 必須賦值總記錄數
}
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
PageSize="5" OnPageChanging="AspNetPager1_PageChanging">
</webdiyer:AspNetPager>
僅設置 PageSize 與事件,控件自動渲染數字鏈接模式。
| 屬性 | 說明 |
|---|---|
| PageSize | 每頁記錄數 |
| RecordCount | 總記錄數(後台賦值) |
| CurrentPageIndex | 當前頁碼 |
| ShowPageIndexBox | 是否顯示跳轉輸入框 |
| CustomInfoHTML | 自定義統計信息模板 |
| OnPageChanging | 分頁事件處理器 |
遷移提示:ASP.NET Core / MVC 專案建議使用
X.PagedList、LazyLoader或前端分頁組件(如 DataTables、Element UI Pagination)配合 API 分頁接口。