datalist分页 :PagedDataSource为datalist分页

PagedDataSource pds = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvBind();
        }
    }
    private void gvBind()
    {
        //获取数据源
        SqlConnection con = new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
        SqlDataAdapter sda = new SqlDataAdapter("select * from jobs", con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        //赋值数据源
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 4;
        int curPage;
        if (Request.QueryString["pg"] != null)
        {
            curPage = Int32.Parse(Request.QueryString["pg"]);//??要判断一下是否为整数
        }
        else
            curPage = 1;
        pds.CurrentPageIndex = curPage - 1;
        this.lbPageNum.Text = curPage.ToString();
        this.lbTotalPage.Text = pds.PageCount.ToString();
        //如果当前页不为首页,则设置“前一页”的URL
        if (!pds.IsFirstPage)
        {
            LinkPre.NavigateUrl = Request.CurrentExecutionFilePath + "?pg=" + Convert.ToString(curPage - 1);

        }
        if (!pds.IsLastPage)
        {
            LinkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?pg=" + Convert.ToString(curPage + 1);

        }
        //设置首页
        LinkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?pg=1";
        LinkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?pg=" + pds.PageCount.ToString();
        DataList1.DataSource = pds;
        DataList1.DataBind();
    }

原文地址:https://www.cnblogs.com/wenming205/p/1228768.html