日期:2014-05-17 浏览次数:20456 次
int pageCount;//总页数
int currentPage = 1;//第定义当前页
void bind()
{
string str = @"Data Source=.\sqlexpress;Initial Catalog=News;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
string sqlStr = "select * from News";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "nid");
//创建数据源
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["nid"].DefaultView;
//允许分页
pds.AllowPaging = true;
//设置每页显示记录数
pds.PageSize = int.Parse(this.DropDownList1.SelectedItem.Value);
//获取总页数
pageCount = pds.PageCount;
this.Label1.Text = pageCount.ToString();
pds.CurrentPageIndex = currentPage - 1;
//当前页
this.Label2.Text = Convert.ToString(currentPage);
this.DataList1.DataSource = pds;
this.DataList1.DataBind();