ASP.NET AspNetPager用DataSet做的分頁

摘要:AspNetPager用DataSet做的分頁

我參考了一下網路教學文章,再用自己的寫法寫成如此紀錄下來

 protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindGridView();
    }
    }

    public void BindGridView()
    {
    SqlConnection cn = new SqlConnection(WebConfigurationManager.ConnectionStrings["test"].ConnectionString);
    cn.Open();
    SqlDataAdapter adaptr = new SqlDataAdapter("select id,class,title,author from test", cn);
    DataSet ds = new DataSet();
    int startRow = (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize;
    adaptr.Fill(ds, startRow, AspNetPager1.PageSize, "test");
    SqlCommand cmd = new SqlCommand("select count(*) from test", cn);
    int redCount =(int) cmd.ExecuteScalar();
    AspNetPager1.RecordCount = redCount;
    GridView1.DataSource = ds;
    GridView1.DataBind();
    AspNetPager1.CustomInfoHTML = "總筆數:" + AspNetPager1.RecordCount.ToString() + "";
    AspNetPager1.CustomInfoHTML += " 總頁數:" + AspNetPager1.PageCount.ToString() + "";
    AspNetPager1.CustomInfoHTML += " 目前頁面:" + AspNetPager1.CurrentPageIndex.ToString() + "";
    cmd.Cancel();
    cmd.Dispose();
    cn.Close();
    cn.Dispose();
    }



    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
    BindGridView();
    }