本来想写成自定义控件的,觉得麻烦,并且暂时没有太多的时间来弄这个。就偷懒,写了个用户控件将就用。
用户控件的html部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | < div style="text-align: right; height: 20px"> < table style="display: inline; float: right;"> < tr > < td style="width: 35px"> < asp:LinkButton ID="BTN_First" runat="server" Text="首页" CommandArgument="First" OnClick="BTN_Pager_Click" /> </ td > < td style="width: 35px"> < asp:LinkButton ID="BTN_Pre" runat="server" Text="上页" CommandArgument="Pre" OnClick="BTN_Pager_Click" /> </ td > < td style="width: 35px"> < asp:LinkButton ID="BTN_Next" runat="server" Text="下页" CommandArgument="Next" OnClick="BTN_Pager_Click" /> </ td > < td style="width: 35px"> < asp:LinkButton ID="BTN_End" runat="server" Text="尾页" CommandArgument="End" OnClick="BTN_Pager_Click" /> </ td > </ tr > </ table > < div style="float: right; margin-right: 10px;"> < asp:TextBox ID="TB_GoTo" runat="server" Width="25px"></ asp:TextBox > < asp:Button ID="BTN_GoTo" runat="server" Text="跳" Height="25px" OnClick="BTN_GoTo_Click" /></ div > < div style="float: right; margin-right: 10px;"> 当前是第< asp:Literal ID="L_PageIndex" Text="1" runat="server" />/< asp:Literal ID="L_PageCount" Text="0" runat="server" />页</ div > < div style="float: right; margin-right: 10px;"> 共有< asp:Literal ID="L_RecordCount" Text="0" runat="server" />条记录</ div > < div style="float: right; margin-right: 10px;"> 每页< asp:Literal ID="L_PageSize" Text="10" runat="server" />条记录</ div > </ div > |
用户控件的后台部分。
定义了一个委托,调用外部根据数据源进行绑定数据的方法,以便在不同的分页应用到不同的场合。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | namespace Fireice.Edo.Web.UserControl { public delegate void PagerDataTable(DataTable dt); public partial class Pager : System.Web.UI.UserControl { protected void Page_Load( object sender, EventArgs e) { if (!Page.IsPostBack) BoundData(); DIV_Pager.Attributes.Add( "style" , _style); //text-align: right; margin-right:40px; margin-bottom:20px; height: 20px } public PagerDataTable PagerDataTableHander; public string TableName { get { if (ViewState[ "TableName" ] == null ) throw new Exception( "没有指定表名" ); else return ViewState[ "TableName" ].ToString(); } set { ViewState[ "TableName" ] = value; } } public string OrderField { get { if (ViewState[ "OrderField" ] == null ) throw new Exception( "没有指定排序的字段" ); return ViewState[ "OrderField" ].ToString(); } set { ViewState[ "OrderField" ] = value; } } public string WherePart { get { return ViewState[ "WherePart" ] == null ? String.Empty : ViewState[ "WherePart" ].ToString(); } set { ViewState[ "WherePart" ] = value; } } public OrderDirect OrderDirect { get { if (ViewState[ "OrderDirect" ] == null ) return OrderDirect.ASC; return (OrderDirect)Enum.Parse( typeof (OrderDirect), ViewState[ "OrderDirect" ].ToString()); } set { ViewState[ "OrderDirect" ] = value; } } public DatabaseType DatabaseType { get { if (ViewState[ "DatabaseType" ] == null ) return DatabaseType.MySql; return (DatabaseType)Enum.Parse( typeof (DatabaseType), ViewState[ "DatabaseType" ].ToString()); } set { ViewState[ "DatabaseType" ] = value; } } private void BoundData() { string tableName = TableName; string orderField = OrderField; string orderDirect = OrderDirect.ToString(); Fireice.Edo.Helper.DB.DBHelper db = new Fireice.Edo.Helper.DB.DBHelper(); //db.GetDataTable(); int count = Convert.ToInt32(db.GetSingleFieldBySQL( "select count(*) from " + tableName + " " + WherePart + " " )); RecordCount = count; PageCount = AccountPageCount(PageSize, RecordCount); string sql = String.Empty; switch (DatabaseType) { case DatabaseType.MySql: sql = " select * from " + tableName + " " + WherePart + " order by " + orderField + " " + orderDirect + " limit " + (PageIndex - 1) * PageSize + "," + PageSize + " " ; break ; case DatabaseType.SqlServer2000: sql = " SELECT TOP " + PageSize + " * FROM " + tableName + " WHERE (" + orderField + " NOT IN (SELECT TOP " + PageSize * (PageIndex - 1) + " " + orderField + " FROM " + tableName + " ORDER BY " + orderField + " " + orderDirect + ")) ORDER BY " + orderField + " " + orderDirect + " " ; break ; } DataTable dt = db.GetDataTable(sql); if (PagerDataTableHander != null ) PagerDataTableHander(dt); //_gv.DataSource = dt; //_gv.DataBind(); } private void ControlState() { if (PageCount <= 1) { TB_GoTo.Enabled = false ; BTN_GoTo.Enabled = false ; } else { BTN_GoTo.Enabled = true ; } if (PageIndex == 1 || PageIndex == 0) { BTN_First.Enabled = false ; BTN_Pre.Enabled = false ; } else { BTN_First.Enabled = true ; BTN_Pre.Enabled = true ; } if (PageIndex == PageCount) { BTN_Next.Enabled = false ; BTN_End.Enabled = false ; } else { BTN_Next.Enabled = true ; BTN_End.Enabled = true ; } } protected override void OnPreRender(EventArgs e) { base .OnPreRender(e); ControlState(); } /// <summary> /// 每页显示的记录数 /// </summary> public int PageSize { get { return Convert.ToInt32(L_PageSize.Text); } set { L_PageSize.Text = value.ToString(); } } /// <summary> /// 总记录数 /// </summary> public int RecordCount { get { return Convert.ToInt32(L_RecordCount.Text); } set { L_RecordCount.Text = value.ToString(); } } /// <summary> /// 当前是第几页 /// </summary> public int PageIndex { get { return Convert.ToInt32(L_PageIndex.Text); } set { L_PageIndex.Text = value.ToString(); } } /// <summary> /// 总页数 /// </summary> public int PageCount { get { return Convert.ToInt32(L_PageCount.Text); } set { L_PageCount.Text = value.ToString(); } } /// <summary> /// 根据总记录数和每页显示的页数计算出总页数 /// </summary> /// <param name="pageSize"></param> /// <param name="recordCount"></param> /// <returns></returns> private int AccountPageCount( int pageSize, int recordCount) { if (pageSize == 0 || recordCount == 0) return 0; else { if (recordCount % pageSize == 0) { return recordCount / pageSize; } else { return recordCount / pageSize + 1; } } } protected void BTN_Pager_Click( object sender, EventArgs e) { string command = ((LinkButton)sender).CommandArgument; switch (command) { case "First" : { PageIndex = 1; break ; } case "Pre" : { PageIndex--; break ; } case "Next" : { PageIndex++; break ; } case "End" : { PageIndex = PageCount; break ; } } BoundData(); } protected void BTN_GoTo_Click( object sender, EventArgs e) { string pageNumber = TB_GoTo.Text; int goToPageNumber = 0; bool check = true ; check = Int32.TryParse(pageNumber, out goToPageNumber); if (goToPageNumber <= 0 || goToPageNumber > PageCount) { check = false ; } if (check) { PageIndex = goToPageNumber; BoundData(); } else { string key = "PagerErrorkey" ; if (!Page.ClientScript.IsStartupScriptRegistered(key)) { Page.ClientScript.RegisterStartupScript( this .GetType(), key, "alert('输入的跳转页数格式不正确或超出范围!');" , true ); } } } private string _style; public string Style { set { _style = value; } } } public enum OrderDirect { DESC, ASC } public enum DatabaseType { SqlServer2000, MySql } } |
具体的使用如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | protected void Page_Load( object sender, EventArgs e) { Pager1.TableName = "accessrecord" ; Pager1.OrderDirect = Fireice.Edo.Web.UserControl.OrderDirect.DESC; Pager1.OrderField = "Recordtime" ; Pager1.PageSize = 20; Pager1.Style = "text-align: right; margin-right:10px; margin-top:10px;margin-bottom:20px; height: 20px" ; Pager1.DatabaseType = Fireice.Edo.Web.UserControl.DatabaseType.MySql; Pager1.PagerDataTableHander += new Fireice.Edo.Web.UserControl.PagerDataTable(BoundGV); } private void BoundGV(DataTable dt) { GridView1.DataSource = dt; GridView1.DataBind(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理