DataList分页代码

public void ShowData()
    {
        SqlConnection con = DB.creCon();
        SqlDataAdapter sda = new SqlDataAdapter(mySql.ListMess(),con);
        DataSet ds = new DataSet();
        sda.Fill(ds,"Temp");

        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = ds.Tables[0].DefaultView;
        objPds.AllowPaging = true;
        objPds.PageSize =15;
        int CurPage;
        if (Request.QueryString["Page"] != null)
        {
            CurPage = Convert.ToInt32(Request.QueryString["Page"]);
        }
        else
        {
            CurPage = 1;
        }
        objPds.CurrentPageIndex = CurPage - 1;
        if (!objPds.IsFirstPage)//响应上一页的按钮
            this.BePage.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
        if (!objPds.IsLastPage)//响应下一页的按钮
            this.NePage.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);

        this.DataList1.DataSource = objPds;//ds.Tables["Temp"].DefaultView;
        this.DataList1.DataBind();
    }

你可能感兴趣的:(datalist)