(续篇).net分页控件webdiyer:aspnetpager与gridview联用

本文探讨了在.NET环境下,使用Webdiyer AspNetPager控件与GridView结合进行分页时遇到的问题及解决方案。当涉及搜索和删除操作时,单纯使用Databind()会导致数据初始化,无法保持搜索条件。作者提出了一种新的方法,通过传递当前页码到Databind函数,解决了页面刷新和分页操作失效的问题。
摘要由CSDN通过智能技术生成

上一篇文章中的使用方法在只有显示,没有其他操作时好用,归根结底是对控件的认识不够,点击分页的时候,页面会刷新一次,造成搜索条件发生后,又重新进行初始化绑定;PagedDataSource pds 存在一个问题,也希望看到这篇文章的大神们可以帮着解决,就是没有搜索或者删除操作时,AspNetPager1_PageChanged事件,直接是Databind(),如果采用下面的代码,每次changed事件,都是在重新加载一遍,而且如果gridview的DataSource不绑定pds,不能点击aspnerpage的页码进行下一页操作。

           

	PagedDataSource pds = new PagedDataSource();
	pds.AllowPaging = true;
	pds.PageSize = AspNetPager1.PageSize;
	pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;//索引同步
 	pds.CurrentPageIndex = 0;
 	pds.DataSource = dt.DefaultView;
 	GridView1.DataSource = pds;//数据绑定
	GridView1.DataBind();
	AspNetPager1.RecordCount = dt.Rows.Count;


参考同事使用的情况,不进行pds操作,直接进行pds操作,只是每次绑定AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);,所以动手将Databind()函数修改为Databind(int index),index传递当前页码,相应的AspNetPager1_PageChanged事件改为Databind(AspNetPager1.CurrentPageIndex),初始化的时候改为Databind(1);

public void Databind(int index)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个分页控件,修改连接字符串,和SQL语句,就可以了。 剩下的直接粘贴进去。就行。 强大的各种属性,附带多种CSS,想怎么分就怎么分页! 拖入工具箱后实现 1.2.3步(你修改下连接符,表就可以了) 控件.aspx页面 复制粘贴到前台页面适当位置即可 <webdiyer:AspNetPager ID="Pager1" runat="server" pagesize="9" CssClass="anpager" onpagechanged="AspNetPager1_PageChanged" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" ShowPageIndexBox="Never"></webdiyer:AspNetPager> 控件.cs页面,后台页面 只需3步 设置连接字符串 string connstring = "Data Source=.;Initial Catalog=数据库名;User ID=用户名;Password=密码"; 程序页面内容:复制粘贴修改几个 //首页加载-1 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection conn = null; try { conn = new SqlConnection(connstring); conn.Open(); SqlCommand Count = new SqlCommand(); Count.Connection = conn; //修改你们查询的表 Count.CommandText = "select count(*) from products"; //查询 Pager1.RecordCount = (int)Count.ExecuteScalar(); //Pager1为分页控件ID BindData(); } finally { conn.Close(); } } } //绑定数据-2 public void BindData() { SqlConnection conn = new SqlConnection(connstring); string sql = "select * from products";//修改你们的查询语句: //select top查询语句 SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, Pager1.PageSize * (Pager1.CurrentPageIndex - 1), Pager1.PageSize, "temptbl"); DataTable dt = ds.Tables["temptbl"]; DataList1.DataSource = dt; DataList1.DataBind(); } //控件事件-每次重新绑定 protected void AspNetPager1_PageChanged(object src, EventArgs e) { BindData(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值