分页c#aspx帮助使用listview

时间:2016-11-16 18:13:21

标签: c# asp.net web-services web

 protected void showproduct()
    {
        int pagecount = 0;
        int currentpage = 0;
        if (Request.QueryString["page"] != null)
        {
            currentpage = Int32.Parse(Request.QueryString["page"]);
        }

        Response.Write("</br>"+currentpage);
        MySqlConnection dbcon = new MySqlConnection("Server=localhost; Database=ip; Uid=root; Password=root;");
        MySqlCommand dbcmd = new MySqlCommand();
        dbcon.Open();
        dbcmd.Connection = dbcon;
        for (int q = 0; q <= currentpage; q++)
        {
            if (currentpage >2 )
            {
                pagecount = q * 3;
            }else if(currentpage==2){
                pagecount = 3;
            }
        }
        dbcmd.CommandText = "select * from product where id >" + pagecount + " order by id asc limit 3";
        //dbcmd.CommandText = "select * from product limit "+ pagecount +",3";
        MySqlDataAdapter adapter = new MySqlDataAdapter(dbcmd.CommandText, dbcon);
        DataTable t = new DataTable();
        adapter.Fill(t);
        ListView1.DataSource = t;
        ListView1.DataBind();

    }

http://prntscr.com/d83mgh 在第2页中,我可以查看

http://prntscr.com/d83msj 单击第3页时,不会显示任何内容

我的资料中有9个数据

1 个答案:

答案 0 :(得分:0)

我假设数据库中的项目具有连续的无空洞ID。

然后,您可以使用所选页面的索引和每页显示的项目数来计算要显示的第一个ID,如下所示:

const int itemsPerPage = 3; // number of items per page to be shown
const int minId = 1; // id of first record in db

// page selected by user in ui
int selectedPage;
if (Request.QueryString["page"] != null) {
    // first page has index 0 to simplify the calculation
    selectedPage  = Int32.Parse(Request.QueryString["page"]) - 1;
}

// id of first record to be shown
int minIdToBeShown = minId + itemsPerPage * selectedPage;

// fetch records including the first record to be shown on the page
dbcmd.CommandText = "select * from product where id >=" + minIdToBeShown 
                  + " order by id asc limit " + itemsPerPage;