• DataList控件实现分页功能

SqlConnection con = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["ConnectionString"]));

string conString = "SELECT [CompanyName], [CustomerID] FROM [Reports_Customers] ORDER BY [CustomerID]";

con.Open();

SqlDataAdapter da = new SqlDataAdapter(conString, con);

DataSet ds = new DataSet();

da.Fill(ds);

PagedDataSource pds = new PagedDataSource();

pds.AllowPaging = true;

pds.PageSize = 10;

pds.DataSource = ds.Tables[0].DefaultView;

Int32 totPage, curPage;

totPage = 0;

curPage = 0;

lnkprev.Visible = true;

lnkfirst.Visible = true;

lnknext.Visible = true;

lnklast.Visible = true;

totPage = pds.PageCount;

if (Request.QueryString["Page"] != null)

{

    curPage = Convert.ToInt32( Request.QueryString["Page"]);

}

else

{

    curPage = 1;

}

pds.CurrentPageIndex = curPage - 1;

lbpage.Text = "当前第" + Convert.ToString(curPage) + "页,共" + Convert.ToString(totPage) + "页";

lnkfirst.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=1";

lnklast.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + totPage.ToString();

if (!pds.IsFirstPage)

{

    lnkprev.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage - 1);

}

else

{

    lnkprev.Visible = false;

    lnkfirst.Visible = false;

}

if (!pds.IsLastPage)

{

    lnknext.PostBackUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(curPage + 1);

}

else

{

    lnknext.Visible = false;

    lnklast.Visible = false;

}

CustomerList.DataSource = pds;

CustomerList.DataBind();

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2022-02-19
  • 2021-09-26
  • 2021-11-12
  • 2021-08-07
  • 2022-12-23
  • 2021-11-16
猜你喜欢
  • 2022-03-03
  • 2021-07-21
  • 2021-09-24
  • 2021-10-11
  • 2022-02-11
  • 2021-11-18
  • 2022-12-23
相关资源
相似解决方案