分页是web开发中频繁用的模块,今天做了一个分页控件,与大家共享,首先说明以下:
1、此控件利用DataSet分页,没用采用存储过程分页,也许不适合大批量数据分页。
2、 此控件需要结合我上一片文章(“c#操作存储过程的通用封装”)使用。
控件前台代码:
<% @ Control Language = " C# " AutoEventWireup = " true " CodeFile = " commPage.ascx.cs " Inherits = " commPage " %> < div > < span > 共 < asp:Label ID = " lblRecordCount " runat = " server " ></ asp:Label > 条 </ span > < span > 当前 < asp:Label ID = " lblCurrentPage " runat = " server " />/< asp:Label ID = " lblPageCount " runat = " server " Text = " Label " /> 页 & nbsp; </ span > < span class = " bia " > < asp:LinkButton ID = " lbnPrevPage " runat = " server " CommandName = " prev " OnCommand = " Page_OnClick " > 上一页 </ asp:LinkButton ></ span > & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; < span class = " bia " >< asp:LinkButton ID = " lbnNextPage "runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:LinkButton></span></div>
控件后台代码:
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls; 10 using System.Web.UI.WebControls; 11 using System.Web.UI.WebControls.WebParts; 12 using System.Xml.Linq; 13 using mjqDb = mjq.zy5777.com; 14 using System.Data.SqlClient; 15 16 public partial class commPage : System.Web.UI.UserControl 17 { 18 // 查询分页数据总数的存储过程名称 19 private string totalNumProc; 20 21 public string TotalNumProc 22 { 23 get { return totalNumProc; } 24 set { totalNumProc = value; } 25 } 26 // 查询分页数据的存储过程名称 27 private string selectProc; 28 29 public string SelectProc 30 { 31 get { return selectProc; } 32 set { selectProc = value; } 33 } 34 35 // 绑定数据的控件 36 private DataList bindControl; 37 38 public DataList BindControl 39 { 40 get { return bindControl; } 41 set { bindControl = value; } 42 } 43 44 private int pageSize; 45 46 public int PageSize 47 { 48 get { return pageSize; } 49 set { pageSize = value; } 50 } 51 52 // 查询分页数据总数的存储过程参数 53 private SqlParameter[] totalParameters; 54 55 public SqlParameter[] Parameters 56 { 57 get { return totalParameters; } 58 set { totalParameters = value; } 59 } 60 // 查询分页数据的存储过程参数 61 private SqlParameter[] selectParameters; 62 63 public SqlParameter[] SelectParameters 64 { 65 get { return selectParameters; } 66 set { selectParameters = value; } 67 } 68 int RecordCount, PageCount, CurrentPage; 69 SqlConnection conn; 70 SqlCommand cmd; 71 protected void Page_Load( object sender, EventArgs e) 72 { 73 74 // 判断是不是第一次请求 75 if ( ! Page.IsPostBack) 76 { 77 ListBind(); 78 CurrentPage = 0 ; 79 ViewState[ " PageIndex " ] = 0 ; 80 81 // 计算总共有多少条记录 82 RecordCount = CalculateRecord(); 83 lblRecordCount.Text = RecordCount.ToString(); 84 85 // 计算总共多少页 86 PageCount = RecordCount / PageSize; 87 lblPageCount.Text = PageCount.ToString(); 88 ViewState[ " PageCount " ] = PageCount; 89 90 } 91 } 92 // 计算总页数 93 public int CalculateRecord() 94 { 95 int intCount; 96 string strCount = TotalNumProc; 97 SqlDataAdapter adpt = mjqDb.execStoreProce.createAdpt(totalNumProc, ref cmd, ref conn, Parameters); 98 DataSet dsC = new DataSet(); 99 adpt.Fill(dsC); 100 // SqlCommand sqlcmd = new SqlCommand(strCount, sqlcon); 101 // SqlDataReader sdr = sqlcmd.ExecuteReader(); 102 DataRow drC = dsC.Tables[ 0 ].Rows[ 0 ]; 103 if (Convert.ToInt32(drC[ 0 ]) > 0 ) 104 { 105 intCount = Convert.ToInt32(drC[ 0 ]); 106 } 107 else 108 { 109 intCount = 0 ; 110 } 111 adpt.Dispose(); 112 dsC.Dispose(); 113 cmd.Dispose(); 114 conn.Close(); 115 return intCount; 116 } 117 ICollection CreateSource() // 分页数据集合 118 { 119 int StartIndex; 120 121 // 设定导入的起终地址 122 StartIndex = CurrentPage * PageSize; 123 string strSel = SelectProc; 124 DataSet ds = new DataSet(); 125 SqlDataAdapter sda = mjqDb.execStoreProce.createAdpt(SelectProc, ref cmd, ref conn, SelectParameters); 126 sda.Fill(ds, StartIndex, PageSize, " jixie " ); // 该句表示将数据源中从StartIndex位置取出PageSize条记录导入DataSet. 127 return ds.Tables[ " jixie " ].DefaultView; 128 129 ds.Dispose(); 130 cmd.Dispose(); 131 conn.Close(); 132 } 133 // 数据绑定 134 public virtual void ListBind() 135 { 136 BindControl.DataSource = CreateSource(); 137 BindControl.DataBind(); 138 lbnNextPage.Enabled = true ; 139 lbnPrevPage.Enabled = true ; 140 if (CurrentPage == (PageCount - 1 )) lbnNextPage.Enabled = false ; 141 if (CurrentPage == 0 ) lbnPrevPage.Enabled = false ; 142 lblCurrentPage.Text = (CurrentPage + 1 ).ToString(); 143 } 144 145 // 上下页 146 public void Page_OnClick(Object sender, CommandEventArgs e) 147 { 148 CurrentPage = ( int )ViewState[ " PageIndex " ]; 149 PageCount = ( int )ViewState[ " PageCount " ]; 150 string cmd = e.CommandName; 151 // 判断cmd,以判定翻页方向 152 switch (cmd) 153 { 154 case " next " : 155 if (CurrentPage < (PageCount - 1 )) CurrentPage ++ ; 156 break ; 157 case " prev " : 158 if (CurrentPage > 0 ) CurrentPage -- ; 159 break ; 160 } 161 ViewState[ " PageIndex " ] = CurrentPage; 162 ListBind(); 163 } 164 }165
调用:
<% @ Register Src = " commPage.ascx " TagName = " commPage " TagPrefix = " uc1 " %> <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > < html xmlns = " http://www.w3.org/1999/xhtml " > < head runat = " server " > < title > 无标题页 </ title > </ head > < body > < form id = " form1 " runat = " server " > < div > < asp:DataList ID = " dataList " runat = " server " > < ItemTemplate > < div > <% #Eval( " title " ) %> </ div > </ ItemTemplate > </ asp:DataList > </ div > < div > < uc1:commPage ID = " commPage1 " runat = " server " /> </ div > </ form > </ body ></html>
在调用页的后台 page_load函数中设置属性:
1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls; 10 using System.Web.UI.WebControls; 11 using System.Web.UI.WebControls.WebParts; 12 using System.Xml.Linq; 13 using mjqDb = mjq.zy5777.com; 14 using System.Data.SqlClient; 15 16 public partial class _Default : System.Web.UI.Page 17 { 18 protected void Page_Load( object sender, EventArgs e) 19 { 20 commPage1.TotalNumProc = " totalProc " ; 21 commPage1.SelectProc = " selectProc " ; 22 commPage1.PageSize = 5 ; 23 commPage1.BindControl = dataList; 24 } 25 }26
转载于:https://www.cnblogs.com/majiangquan/archive/2009/04/20/1439832.html
相关资源:asp.net c# 界面美观,功能强大的.net分页控件