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

asp.net分页函数
private int pageIndex = 1;
  private int pageSize = 17;
  private int pageCount = -1;
  private int recordCount = -1;
  private string WhereString = string.Empty;
   
  protected void Page_Load(object sender, EventArgs e)
  {
  if (Request.QueryString["searchtext"] != null)
  {
  string searchtext = Request.QueryString["searchtext"].ToString().Trim();
  this.txtSearch.Text = searchtext;
  Bind公司列表(1, 17, " where 公司名 like '%%" + searchtext + "%%'");
   
  }

  if (!IsPostBack && Request.QueryString["searchtext"]==null)
  {
  Bind公司列表(1, 17, string.Empty);
  }
  }

  private void Bind公司列表(int 页数, int 每页显示数, string 条件)
  {
  string strQuery = "select * from 办公楼租户";
  DataTable dt = m_Pager.ExecutePager(页数, 每页显示数, "id,楼层,房间号,公司名,公司简介,联系电话,商标图片", strQuery, 条件, "楼层 asc,房间号 asc", out pageCount, out recordCount);

  if (pageCount <= pageIndex)
  {
  this.btn下一页.Enabled = false;
  }
  else
  {
  this.btn下一页.Enabled = true;
  }

  if (pageIndex == 1)
  {
  this.btn上一页.Enabled = false;
  }
  else
  {
  this.btn上一页.Enabled = true;
  }

  gdv公司列表.DataSource = dt;
  gdv公司列表.DataBind();
  }
  protected void btn首页_Click(object sender, ImageClickEventArgs e)
  {
  pageIndex = 1;
  Bind公司列表(pageIndex, 17, WhereString);
  }
  protected void btn上一页_Click(object sender, ImageClickEventArgs e)
  {
  pageIndex = pageIndex - 1;
  Bind公司列表(pageIndex, 17, WhereString);
  }
  protected void btn尾页_Click(object sender, ImageClickEventArgs e)
  {
  string strSql = " select count(0) as recordCount from ( select * from 办公楼租户 ) tempVw ";
  recordCount = Convert.ToInt32(AccessHelper.ExecuteScalar(strSql));

  if ((recordCount % pageSize) > 0)
  {
  pageCount = recordCount / pageSize + 1;
  }
  else
  {
  pageCount = recordCount / pageSize;
  }

  pageIndex = pageCount;
  Bind公司列表(pageIndex, 17, WhereString);
  }
  protected void btn下一页_Click(object sender, ImageClickEventArgs e)
  {
  pageIndex = pageIndex + 1;
  Bind公司列表(pageIndex, 17, WhereString);
  }

为什么我的分页函数只能翻页到第二页无法到第三页,同样在第三页返回上页却到了第一页?

------解决方案--------------------
你在 load 事件之前定义的变量 要定义成 静态变量
private static int pageIndex = 1;
private static int pageSize = 17;
private static int pageCount = -1;
private static int recordCount = -1;


------解决方案--------------------
把楼上说的那些弄到隐藏域里面去:
HTML code

<asp:HiddenField ID="HiddenField1" Value="1" runat="server" />