一个简单的数据行分页用户控件时间:2021-02-10 08:07:10指定好页面大小,每页显示标记个数 ,传入总行数计算出 分页后开始行和结束行的索引! 下面是代码! 分页控件后台代码 #region "命名空间" using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; #endregion /// <summary> /// 分页控件 /// </summary> public partial class Pagination : System.Web.UI.UserControl { #region "初始化属性值" /// <summary> /// 构造函数 /// </summary> public Pagination() { _CurrentPageIndex = 1; //默认从第一页开始显示 _PageSize = 10; _PageTotal = 0; _ShowPageSize = 5; _RecordTotal = 0; } #endregion #region "字段属性" public delegate void SubmitUserHandler(object sender, SubmitUserEventArgs e); /// <summary> /// 点击分页控件上链接时的事件 /// </summary> public event SubmitUserHandler SubmitUserEvent; /// <summary> /// 分页控件中部导航页码的格式 /// </summary> private const string NumberStyle = "[{0}]"; private const string PageInfoStyle = "[{0}/{1}] 每页{2}条 共{3}条"; private int _CurrentPageIndex; /// <summary> /// 获取或设置当前页号 /// </summary> public int CurrentPageIndex { get { if (ViewState["CurrentPageIndex"] != null) _CurrentPageIndex = (int)ViewState["CurrentPageIndex"]; return _CurrentPageIndex; } set { ViewState["CurrentPageIndex"] = value; } } private int _PageSize; /// <summary> /// 获取或设置页面大小 /// </summary> public int PageSize { get { return _PageSize; } set { _PageSize = value; } } private int _ShowPageSize; /// <summary> /// 获取或设置显示导航标记的个数 /// </summary> public int ShowPageSize { get { return _ShowPageSize; } set { _ShowPageSize = value; } } private int _PageTotal; /// <summary> /// 总页面数 /// </summary> public int PageTotal { get { return _PageTotal; } set { _PageTotal = value; } } private int _RecordTotal; /// <summary> /// 获取或设置总记录数 /// </summary> public int RecordTotal { get { return _RecordTotal; } set { _RecordTotal = value; } } private string _selectedStyle; /// <summary> /// 选中页样式 /// </summary> public string SelectedStyle { get { return _selectedStyle; } set { _selectedStyle = value; } } #endregion #region "页面事件" /// <summary> /// 载入页面 /// </summary> protected void Page_Load(object sender, EventArgs e) { StratInitVar(); CalRowRegion(); CreateLinkButton(); } /// <summary> /// 页面功能按钮通用事件 /// </summary> protected void btnCmd_Command(object sender, CommandEventArgs e) { switch (e.CommandName) { case "First": CurrentPageIndex = 1; break; case "Next": CurrentPageIndex += 1; break; case "Previous": CurrentPageIndex -= 1; break; case "Last": CurrentPageIndex = PageTotal; break; case "GoPage": CurrentPageIndex = int.Parse(e.CommandArgument.ToString()); break; } this.phPage.Controls.Clear(); CreateLinkButton(); CalRowRegion(); } /// <summary> /// 转到按钮事件,转到指定页面,如果输入的数字大于总页数 则显示末页,如果小于1则显示首页 /// </summary> protected void btnGo_Click(object sender, EventArgs e) { try { int number = int.Parse(txtPageNumber.Text); if (number > PageTotal) { number = PageTotal; } else if (number < 1) { number = 1; } CurrentPageIndex = number; this.phPage.Controls.Clear(); CreateLinkButton(); CalRowRegion(); } catch { txtPageNumber.Text = ""; } } #endregion #region "辅助功能方法" /// <summary> /// 初始化计算总页面数 /// </summary> private void StratInitVar() { PageTotal = _RecordTotal % _PageSize == 0 ? _RecordTotal / _PageSize : _RecordTotal / _PageSize + 1; } /// <summary> /// 计算当前页所在行区间,并将结果通过事件传至调用页面 /// </summary> private void CalRowRegion() { int StartRowIndex = 1; int EndRowIndex = RecordTotal; if (PageTotal > 1) { StartRowIndex = (CurrentPageIndex - 1) * PageSize + 1; if (RecordTotal <= CurrentPageIndex * PageSize) // 当前页号和页面大小的乘积大于实际记录数时取实际数 EndRowIndex = RecordTotal; else EndRowIndex = CurrentPageIndex * PageSize; } SubmitUserEvent(this, new SubmitUserEventArgs(StartRowIndex, EndRowIndex));//调用事件方法,通过事件参数传递值 ShowPageInfo(); } /// <summary> /// 设置控件可用状态 /// </summary> private void SetControlState() { if (CurrentPageIndex == 1) {//如果在第一页 btnFirst.Enabled = false; btnPrevious.Enabled = false; btnNext.Enabled = true; btnLast.Enabled = true; } else if (CurrentPageIndex > 1 && CurrentPageIndex < PageTotal) { btnFirst.Enabled = true; btnPrevious.Enabled = true; btnNext.Enabled = true; btnLast.Enabled = true; } else { //如果在最后一页 btnFirst.Enabled = true; btnPrevious.Enabled = true; btnLast.Enabled = false; btnNext.Enabled = false; } } /// <summary> /// 设置,当前页按钮样式 /// </summary> /// <param name="btnSelected">LinkButton</param> /// <param name="PageNumber">页号</param> /// <returns></returns> private LinkButton SetStyle(LinkButton btnSelected, int PageNumber) { if (PageNumber == CurrentPageIndex) { btnSelected.Attributes["style"] = SelectedStyle; } else { btnSelected.Attributes["style"] = ""; } return btnSelected; } /// <summary> /// 显示分页信息 /// </summary> private void ShowPageInfo() { lblPageInfo.Text = string.Format(PageInfoStyle, CurrentPageIndex, PageTotal, PageSize, RecordTotal); } #endregion #region "分页链接生成" /// <summary> /// 创建分页链接 /// 说明:当前页,显示的链接保持居中,如果当前页的导航标记在头尾两端时,则左右互相补齐到指定大小 /// </summary> private void CreateLinkButton() { int left = 0, right = 0;//left 是当前页到左边标记开始处 个数,right是当前页岛右边标记结束个数 if (ShowPageSize % 2 == 0) { left = ShowPageSize / 2 - 1; //如果显示的标记为偶数个,则左边显示比右边多一个 right = ShowPageSize / 2; } else { left = right = (ShowPageSize - 1) / 2; } if (CurrentPageIndex - left < 1) { //如果按钮在起始位置附近 left = CurrentPageIndex - 1; right = ShowPageSize - CurrentPageIndex; } else if (CurrentPageIndex + right > PageTotal) { //如果按钮在结束位置附近 right = PageTotal - CurrentPageIndex; left = ShowPageSize - right - 1; } int stratIndex = CurrentPageIndex - left; int endIndex = CurrentPageIndex + right; for (int i = stratIndex; i <= endIndex; i++) { LinkButton btnGoPage = new LinkButton(); btnGoPage.CommandName = "GoPage"; btnGoPage.ID = "page_lt" + i.ToString();//必须指定控件ID btnGoPage.CommandArgument = i.ToString(); btnGoPage.Text = string.Format(NumberStyle, i); btnGoPage.Command += new CommandEventHandler(btnCmd_Command); SetStyle(btnGoPage, i); phPage.Controls.Add(btnGoPage); } SetControlState(); } #endregion #region "事件参数" /// <summary> /// 事件参数 /// </summary> public class SubmitUserEventArgs : EventArgs { public SubmitUserEventArgs(int stratRowIndex, int endRowIndex) { _stratRowIndex = stratRowIndex; _endRowIndex = endRowIndex; } private int _stratRowIndex; /// <summary> /// 获取当前页开始行索引值 /// </summary> public int StratRowIndex { get { return _stratRowIndex; } } /// <summary> /// 获取当前页结束行索引值 /// </summary> private int _endRowIndex; public int EndRowIndex { get { return _endRowIndex; } } } #endregion } 分页控件前台代码 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Pagination.ascx.cs" Inherits="Pagination" %> <span> <asp:LinkButton ID="btnFirst" runat="server" OnCommand="btnCmd_Command" CommandName="First">首页</asp:LinkButton> <asp:LinkButton ID="btnPrevious" runat="server" CommandName="Previous" OnCommand="btnCmd_Command">上一页</asp:LinkButton> <asp:PlaceHolder ID="phPage" runat="server"></asp:PlaceHolder> <asp:LinkButton ID="btnNext" runat="server" OnCommand="btnCmd_Command" CommandName="Next">下一页</asp:LinkButton> <asp:LinkButton ID="btnLast" runat="server" OnCommand="btnCmd_Command" CommandName="Last">尾页</asp:LinkButton> <asp:TextBox ID="txtPageNumber" runat="server" Width="30"></asp:TextBox> <asp:LinkButton ID="btnGo" runat="server" OnClick="btnGo_Click">转到</asp:LinkButton> <asp:Label ID="lblPageInfo" runat="server"></asp:Label> </span> 调用页面后台代码 protected void Page_Load(object sender, EventArgs e) { Pagination1.SubmitUserEvent += new Pagination.SubmitUserHandler(Pagination1_SubmitUserEvent); Pagination1.SelectedStyle = "color:red"; Pagination1.RecordTotal = GetDt().Rows.Count; } public void Pagination1_SubmitUserEvent(object sender, Pagination.SubmitUserEventArgs e) { Label1.Text = string.Format("起始行:{0},结束行:{1}", e.StratRowIndex, e.EndRowIndex); GridView1.DataSource = GetDt(GetDt(), e.StratRowIndex, e.EndRowIndex).DefaultView; GridView1.DataBind(); } private DataTable GetDt() { DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Value"); for (int i = 1; i <= 222; i++) { dt.Rows.Add(new object[] { i.ToString(), "V" + i.ToString() }); } return dt; } private DataTable GetDt(DataTable dt, int strat, int end) { DataTable newdt = dt.Clone(); for (int i = strat - 1; i < end; i++) { newdt.Rows.Add(new object[] { dt.Rows[i][0], dt.Rows[i][1] }); } return newdt; } 调用页面前台代码 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"> </asp:GridView> <asp:Panel ID="panel" runat="server"> <uc1:Pagination ID="Pagination1" runat="server" PageSize="5" ShowPageSize="7" /> </asp:Panel> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 效果如下