datalist 分页

private void indexListBind()

    {

        try

        {

            string parID = Request.QueryString["id"].ToString();

            int curPage = Convert.ToInt32(this.lb_pageIndex.Text) - 1;//获取当前页码,我用的是一个lable.当了中间变量

            con = Admin_DB.getConn();

            con.Open();

            DataSet ds = new DataSet();

            string Path = "~";

            OleDbDataAdapter sda = new OleDbDataAdapter("select ID,'" + Path + "'+imagePath as imagePath,introduce from D_Picture where parID=@parID", con);

            sda.SelectCommand.Parameters.Add(new OleDbParameter("@parID", parID));

            sda.Fill(ds, "index");

            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();//实例化分页数据源

            ps.DataSource = ds.Tables["index"].DefaultView;//将要绑定在datalist上的datatable给分页数据源

            ps.AllowPaging = true;

            ps.PageSize = 8;//每页显示几条记录

            ps.CurrentPageIndex = curPage;//设置当前页的索引(当前页码减1就是)

            this.btn_up.Enabled = true;

            this.btn_next.Enabled = true;

            this.btn_frist.Enabled = true;

            this.btn_end.Enabled = true;

            endPage = ps.PageCount;

            if (curPage == 0)//当是第一页是.上一页和首页的按钮不可用

            {

                this.btn_up.Enabled = false;

                this.btn_frist.Enabled = false;

            }

            if (curPage == ps.PageCount - 1)//当是最后一页时下一页和最后一页的按钮不可用

            {

                this.btn_next.Enabled = false;

                this.btn_end.Enabled = false;

            }

            this.indexList.DataSource = ps;

            this.indexList.DataKeyField = "ID";

            this.indexList.DataBind();

            con.Close();

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

        }

 

    }

    protected void btn_frist_Click(object sender, EventArgs e)

    {

        this.lb_pageIndex.Text = "1";

        this.indexListBind();

    }

    protected void btn_up_Click(object sender, EventArgs e)

    {

        int page = int.Parse(this.lb_pageIndex.Text) - 1;

        this.lb_pageIndex.Text = page.ToString();

        this.indexListBind();

    }

    protected void btn_next_Click(object sender, EventArgs e)

    {

        int page = int.Parse(this.lb_pageIndex.Text) + 1;

        this.lb_pageIndex.Text = page.ToString();

        this.indexListBind();

    }

    protected void btn_end_Click(object sender, EventArgs e)

    {

        this.lb_pageIndex.Text = endPage.ToString();

        this.indexListBind();

}

posted @ 2011-06-01 15:09  俗雅冰山  阅读(155)  评论(0编辑  收藏  举报