日期:2014-05-18  浏览次数:20519 次

vs2005 AspNetPage 分页控件的分页问题(问题出数据帮定上!)
如题~

代码部分:

  protected void Page_Load(object sender, EventArgs e)
  {
  if (!Page.IsPostBack)
  {
   
  SqlConnection conn = newsOperate.getConn();
  conn.Open();
  SqlCommand cmd = new SqlCommand("select count(nid) from serectNews",conn);
  //设置要分页的数据的总数  
  pager.RecordCount = (int)cmd.ExecuteScalar();
  conn.Close();
  bindData();
  fillNews();
   
  }
   
  }

  public void bindData()
  {
  SqlConnection conn = newsOperate.getConn();
  conn.Open();
  SqlCommand cmd = new SqlCommand("select nid,title,classid,addtime from serectNews order by nid desc", conn);
  SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  //注意下面这句,只填充当前页要显示的数据,不能把所有数据全填充到DataSet中,否则无法实现分页  
  adapter.Fill(ds, pager.PageSize * (pager.CurrentPageIndex - 1), pager.PageSize, "serectNews");
  GridView1.DataSource = ds.Tables["serectNews"];
  //GridView1.DataBind(); //老大请看这,我只有注释掉他代码才可以执行,不然会显示一个错误:异常详细信息: System.Web.HttpException: 在选定的数据源上未找到名为“classname”的字段或属性。


  }

  protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
  {
  pager.CurrentPageIndex = e.NewPageIndex;
  bindData();
  }

前台代码:

<webdiyer:AspNetPager ID="pager" runat="server" AlwaysShow="True" UrlPaging="True" ShowCustomInfoSection="Right" OnPageChanging="pager_PageChanging">
  </webdiyer:AspNetPager>

当点击了下一页的时候,状态栏显示的地址是可以改变的
xx.aspx?page=1 可以得到 xx.aspx?page=2
但是有个问题就是 GridView里的数据没有随着 分页而分页。

最后想说的就是,分不是很多,但是这个问题困绕我1天了,早上4点04分!
不知道我有没有说清楚!谢谢

------解决方案--------------------
昏,你去掉了绑定,那还有用的啊。是不是sql语句有问题啊。select nid,title,classid,addtime from serectNews order by nid desc这里的字段搞错了吧。
------解决方案--------------------
//GridView1.DataBind();
这句不能取消的,至于报错你就要检查你的SQL语句了
------解决方案--------------------
首先在对gridview进行绑定一定要加上GridView1.DataBind()
你的问题多数出在你在前台写的列名有问题.