这段分页代码有个问题。当我把sql语句改变条件的时候就会出现异常
比如:string strSQL = "select count(*) from pagination where UserName='dnvvj'";
我查找用户名为dnvvj的数据。 在前几页都没有问题,因为我设置的是每页显示5条数据。
可是到最后一页的时候,比如数据不足五条的时候,就会用其他数据填充。来凑满五条。
可是这就不是我希望显示的内容了。  

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class pagination : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
    public int PageSize = 5; //设置每页显示多少条记录
    protected void Page_Load(object sender, EventArgs e)
    {
        conn.Open();        string strSQL = "select count(*) from pagination where UserName='dnvvj'";
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
        int Page = Convert.ToInt32(Request.QueryString.Get("PageID")); //获取页码
        if (Page == 0)
        {
            Page = 1;
        }
        float temp = (float)MaxSize / PageSize;
        if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
        {
            Page = Convert.ToInt32(Math.Ceiling(temp));
        }
        string SQL = "select top " + PageSize + " * from pagination where UserID>" + (Page - 1) * PageSize;
        SqlCommand scmd = new SqlCommand(SQL, conn);
        SqlDataReader sdr = scmd.ExecuteReader();
        string html = "<table border=1>";
        html += "<tr>";
        html += "<td width=150px>用户名</td>";
        html += "<td width=100px>密码</td>";
        html += "<td width=250px>地址</td>";
        html += "</tr>";        while (sdr.Read())
        {
            html += "<tr>";
            html += "<td width=150px>" + sdr["UserName"].ToString() + "</td>";
            html += "<td width=100px>" + sdr["UserPwd"].ToString() + "</td>";
            html += "<td width=250px>" + sdr["UserAddr"].ToString() + "</td>";
            html += "</tr>";
        }        html += "</table>";        sdr.Close();
        conn.Close();        show.Text = html;
        LblTitle.Text = "共有" + Convert.ToInt32(Math.Ceiling(temp)) + "页/当前是第" + Page + "页";
    }    protected void BtnFirst_Click(object sender, EventArgs e)
    {
        Response.Redirect("pagination.aspx?PageID=1");
    }    protected void BtnPre_Click(object sender, EventArgs e)
    {
        int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
        if (Page <= 1)
        {
            Page = 2;
        }
        int curent = Page - 1;
        Response.Redirect("pagination.aspx?PageID=" + curent);
    }    protected void BtnNext_Click(object sender, EventArgs e)
    {
        conn.Open(); 
        string strSQL = "select count(*) from pagination where UserName='dnvvj'";
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
        int Page = Convert.ToInt32(Request.QueryString.Get("PageID"));
        float temp = (float)MaxSize / PageSize;
        if (Page >= Convert.ToInt32(Math.Ceiling(temp)))
        {
            Page = Convert.ToInt32(Math.Ceiling(temp)) - 1;
        }
        if (Page <= 0)
        {
            Page = 1;
        }
        int curent = Page + 1;
        Response.Redirect("pagination.aspx?PageID=" + curent);
        conn.Close();
    }    protected void BtnLast_Click(object sender, EventArgs e)
    {
        conn.Open();
        string strSQL = "select count(*) from pagination where UserName='dnvvj'";
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        int MaxSize = Convert.ToInt32(cmd.ExecuteScalar()); //取出总笔数 
        float temp = (float)MaxSize / PageSize;
        Response.Redirect("pagination.aspx?PageID=" + Math.Ceiling(temp));
        conn.Close();
    }
}

解决方案 »

  1.   

    改一下这句就ok了string SQL = "select top " + PageSize + " * from pagination where UserName='dnvvj' and UserID>" + (Page - 1) * PageSize;
      

  2.   

    int iTop = PageSize==temp?MaxSize-(PageIndex-1)*PageSize:PageSize;
    string SQL = "select top " + iTop.ToString()+ " * from pagination where UserID>" + (Page - 1) * PageSize;
    判断一下是否为最后一页,最后一页只取需要的