打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ASP.NET 数据列表控件的分页总结(一):自定义方法分页和PageDataSource
在Asp.net中,提供了三个功能强大的列表控件:GridView、DataList和Repeater控件,但其中只有GridView控件提供分页功能。虽然DataGrid提供了分页功能,不过看上去功能有限,但是我们可以通过GridView的一些属性来获取状态以及增加首页、尾页功能按钮。如果在速度效率不是很讲究的情况下,由DataGrid自己管理分页还是不错的,付出的代价就是要把整个相关数据取出来后再删选指定页的数据。好处就是开发速度快,不需要写分页的存储过程。所以若需要追求执行效率,而且数据量比较大的情况下建议使用GridView的自定义分页功能。若数据量不是很大,需要追求更多的页面功能和样式,那么相对GridView来说,DataList和Repeater控件具有更高的样式自定义性,所以很多时候我们喜欢使用DataList或Repeater控件来显示数据。现在我采用手动分页,对这三个控件作一比较。如下: (1).使用GridView手动分页。通过下拉框来控制分页。前台代码: Code 1 2 11 后台代码: Code using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { static SqlConnection con; static SqlDataAdapter sda; static SqlCommand cmd; static DataSet ds; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); //记住这句不能丢,否则行号会累加在下拉框中 DropDownList1.Items.Clear(); for (int i = 1; i< gvShow.PageCount; i++) { DropDownList1.Items.Add(i.ToString()); } } } private void Bind() { //gvShow.AllowPaging = true; //gvShow.PageSize = 2; gvShow.DataSource =BindData(); gvShow.DataBind(); } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { //关键代码 gvShow.PageIndex = Convert.ToInt32(DropDownList1.SelectedValue)-1; Bind(); } protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvShow.PageIndex = e.NewPageIndex; Bind(); } private static void Init() { //数据库连接语句 con = new SqlConnection("Data Source=.;database=数据库名;uid=用户ID;pwd=用户密码"); ds = new DataSet(); try { con.Open(); } catch { throw new Exception(); } } public static DataSet BindData() { Init(); string sql = "select * from book"; try { sda = new SqlDataAdapter(sql, con); sda.Fill(ds, "book"); return ds; } catch { throw new Exception(); } } } (2).使用DataList手动分页。通过下拉框来控制分页。前台代码: Code 1 2 53 后台代码: Code 1using System; 2using System.Data; 3using System.Configuration; 4using System.Web; 5using System.Web.Security; 6using System.Web.UI; 7using System.Web.UI.WebControls; 8using System.Web.UI.WebControls.WebParts; 9using System.Web.UI.HtmlControls; 10using System.Data.SqlClient; 11 12 13public partial class _Default : System.Web.UI.Page 14{ 15 private static int PageIndex;//保存当前页的索引 16 private static int pagesize;//保存每个页面中的记录数目 17 private static int CurrentPage;//为当前页序号-1 18 private static int PageCount;//保存总页数 19 private static int RecordCount;//总记录数 20 21 public bool DropFill; 22 23 protected void Page_Load(object sender, EventArgs e) 24 { 25 if (!IsPostBack) 26 { 27 DropFill = true; 28 pagesize = 10;//pagesize = MyDataGrid.RepeatColumns;可以设DataList的属性RepeatDirection="Horizontal" RepeatColumns="10",通过RepeatColumns来设置每页显示的条数 29 CurrentPage = 0; 30 GetPageCount();//得到总记录数 31 Databind(); 32 } 33 34 } 35 public void Databind() 36 { 37 D1.Items.Clear(); 38 string sql = "select * from Customers"; 39 40 SqlConnection con=new SqlConnection ("server=.;database=Northwind;uid=sa"); 41 SqlCommand cmd = new SqlCommand(sql, con); 42 43 SqlDataAdapter da = new SqlDataAdapter(cmd); 44 DataSet ds = new DataSet(); 45 PageIndex = CurrentPage * pagesize; 46 da.Fill(ds, PageIndex, pagesize, "table"); 47 48 MyDataGrid.DataSource = ds.Tables["table"]; 49 MyDataGrid.DataBind(); 50 con.Close(); 51 PageCount = RecordCount / pagesize; 52 53 if (RecordCount % pagesize != 0) 54 PageCount++; 55 lalPageCount.Text = PageCount.ToString(); 56 lblRecordCount.Text = RecordCount.ToString(); 57 58 if (lblCurrentPage.Text == "") 59 { 60 lblCurrentPage.Text = "1"; 61 } 62 63 if (CurrentPage == 0) 64 { 65 btnFrist.Enabled = false; 66 btnPrev.Enabled = false; 67 } 68 if (CurrentPage == PageCount - 1) 69 { 70 btnLast.Enabled = false; 71 btnNext.Enabled = false; 72 } 73 74 //给下拉链表中添加页数 75 if (DropFill == true) 76 { 77 for (int i = 1; i <= PageCount; i++) 78 { 79 D1.Items.Add(new ListItem(i.ToString())); 80 } 81 } 82 else 83 { 84 D1.Items.Add("1"); 85 } 86 87 } 88 public void GetPageCount() 89 { 90 string sql = "select * from Customers "; 91 92 SqlConnection c = new SqlConnection("server=.;database=Northwind;uid=sa"); 93 SqlCommand cm = new SqlCommand(sql, c); 94 95 SqlDataAdapter da = new SqlDataAdapter(cm); 96 DataSet ds = new DataSet(); 97 da.Fill(ds, "table"); 98 99 RecordCount = ds.Tables["table"].DefaultView.Count; 100 c.Close(); 101 } 102 protected void D1_SelectedIndexChanged(object sender, EventArgs e) 103 { 104 btnFrist.Enabled = true; 105 btnLast.Enabled = true; 106 btnNext.Enabled = true; 107 btnPrev.Enabled = true; 108 //DropFill = false; 109 110 CurrentPage = Int32.Parse(D1.SelectedValue.ToString()) - 1;//当前页 111 DropFill = true; 112 Databind(); 113 lblCurrentPage.Text = (CurrentPage + 1).ToString(); 114 115 } 116 117 protected void PagerButtonClick(object sender, CommandEventArgs e) 118 { 119 btnFrist.Enabled = true; 120 btnLast.Enabled = true; 121 btnNext.Enabled = true; 122 btnPrev.Enabled = true; 123 DropFill = true; 124 125 string age = e.CommandArgument.ToString(); 126 switch (age) 127 { 128 case "Next"://后一页 129 if (CurrentPage < PageCount - 1) 130 { 131 CurrentPage++; 132 } 133 break; 134 case "Prev"://前一页 135 if (CurrentPage > 0) 136 { 137 CurrentPage--; 138 } 139 break; 140 case "Frist"://首页 141 { 142 CurrentPage = 0; 143 } 144 break; 145 case "Last"://尾页 146 { 147 CurrentPage = PageCount - 1; 148 } 149 break; 150 } 151 Databind(); 152 lblCurrentPage.Text = Convert.ToString(CurrentPage + 1); 153 } 154} 155 (3).使用Repeater手动分页。前台代码: Code 1 2 25 后台代码: Code 1using System; 2using System.Data; 3using System.Configuration; 4using System.Web; 5using System.Web.Security; 6using System.Web.UI; 7using System.Web.UI.WebControls; 8using System.Web.UI.WebControls.WebParts; 9using System.Web.UI.HtmlControls; 10using System.Data.SqlClient; 11public partial class _Default : System.Web.UI.Page 12{ 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 SqlConnection Conn = new SqlConnection("server=.;database=pubs;uid=sa;pwd="); 16 SqlDataAdapter sda = new SqlDataAdapter("select * from authors", Conn); 17 DataSet ds = new DataSet(); 18 sda.Fill(ds, "users"); 19 20 //对PagedDataSource 对象的相关属性赋值 21 PagedDataSource pds = new PagedDataSource(); 22 //数据源指向定义好的DataSet 23 pds.DataSource = ds.Tables["users"].DefaultView; 24 //AllowPaging属性设置是否允许分页 25 pds.AllowPaging = true; 26 //设置每页条数 27 pds.PageSize = 2; 28 int CurPage; 29 30 //当前页面从Page查询参数获取 31 if (Request.QueryString["Page"] != null) 32 CurPage = Convert.ToInt32(Request.QueryString["Page"]); 33 else 34 CurPage = 1; 35 36 pds.CurrentPageIndex = CurPage - 1; 37 //显示当前页数 38 lblCurrentPage.Text = "当前是第: " + CurPage.ToString() + "页"; 39 //显示共有的页数 40 lblCount.Text = "共有" + Convert.ToString(pds.PageCount) + "页"; 41 42 //向前翻页 43 if (!pds.IsFirstPage) 44 lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1); 45 46 //向后翻页 47 if (!pds.IsLastPage) 48 lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1); 49 50 //把PagedDataSource 对象赋给Repeater控件 51 Repeater1.DataSource = pds; 52 Repeater1.DataBind(); 53 } 54} 55 总结:上面前2种是采用DropDownList自定义一个方法的简单分页,最后一个是采用PagedDataSource类来分页。采用PagedDataSource类,效率要低些,每次都要把所有页的数据都select出来,若用一个方法或者存储过程的话,仅仅只select出当前页的数据,效率上就高些。至于对控件的使用,若需要追求执行效率,而且数据量比较大的情况下建议使用GridView的自定义分页功能。若数据量不是很大,需要追求更多的页面功能和样式,那么可以考虑使用DataList和Repeater控件。感谢你阅读本文,希望这篇文章给你带来帮助!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
GridView激发了未处理事件PageIndexChanging
gridview分页模型
MOSS 2007基础:开发自定义WebPart
VB.NET版的GridView经典使用(编辑,删除,分页,链接列)
导出Excel出现Microsoft Office Excel 不能访问文件
DataGrid终极分页法:给分页加上总记录数、总页数、当前页数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服