DataTable实现上一页,下一页效果


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
原理:在DataTable中构造一个pageBy 自增字段,, 根据页面传来的值查出当前行对应的pageBy,那么减1就是上一条,加1就是上一条, 实际是为DataTable的Row实现索引。
1,构造索引:pageBy
        DataTable pd = dt.Clone();  //dt为数据表
        DataColumn column = new DataColumn("pageBy");
        column.DataType = System.Type.GetType("System.Int32");
        column.AutoIncrement = true;
        column.AutoIncrementSeed = 1;
        column.AutoIncrementStep = 1;
        pd.Columns.Add(column);
        DataColumn[] columns = new DataColumn[1];
        columns[0] = column;
        pd.PrimaryKey = columns;
        
        foreach (DataRow dr in dt.Rows) {
            pd.ImportRow(dr);  //引入数据
        }
  Session["pageData"] = pd; //存入Session中,以便查询

2,列表页面传来选中行的实际id, 查出pageBy
        DataTable dt = (DataTable)Session["pageData"];
        DataRow[] drs = new DataRow[1];
        drs = dt.Select("id=" + allocateid);   //根据页面传来的id查出pageBy
        if (drs.Length > 0)
        {
            int pageIndex = int.Parse(drs[0]["pageBy"].ToString());
            //上一页Callback.aspx?allocateid=226&customerid=67
            drs = dt.Select("pageBy=" + (pageIndex -1).ToString() );
            if (drs.Length <= 0)
            {
                hlPrevious.Text = "上一条:没有了";
            }
            else {
                hlPrevious.Text = "上一条:" + drs[0]["CustomerName"].ToString();
                hlPrevious.NavigateUrl = "Callback.aspx?allocateid=" + drs[0]["id"] + "&customerid=" + drs[0]["CustomerID"];
            }
            //下一页
            drs = dt.Select("pageBy=" + (pageIndex + 1).ToString());
            if (drs.Length <= 0)
            {
                hlNext.Text = "下一条:没有了";
            }
            else
            {
                hlNext.Text = "下一条:" + drs[0]["CustomerName"].ToString();
                hlNext.NavigateUrl = "Callback.aspx?allocateid=" + drs[0]["id"] + "&customerid=" + drs[0]["CustomerID"];
            }