AspNetPager的用法
public partial class PagedIndex4 : System.Web.UI.Page
{
private Current_db db = new Current_db();
SqlConnection conn;
SqlCommand cmd;
private void Page_Load(object sender, System.EventArgs e)
{
conn = new SqlConnection("Server = 192.168.1.15;database=my_sz5_cn;uid=skyray;pwd=skyray");
if (!Page.IsPostBack)
{
cmd = new SqlCommand("select count(*) from info_information",conn);
conn.Open();
//设置要分页的数据的总数
lbtotal.Text = cmd.ExecuteScalar().ToString();
AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
lbs.Text = AspNetPager1.PageSize.ToString();
//int PageCount = AspNetPager1.RecordCount / AspNetPager1.PageSize+1;
//lbPages.Text = PageCount.ToString();
conn.Close();
//绑定数据
BindData();
}
}
void BindData()
{
cmd = new SqlCommand("select top 10000 * from info_information", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
//注意下面这句,只填充当前页要显示的数据,不能把所有数据全填充到DataSet中,否则无法实现分页
adapter.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSiz e, "info_information");
GridView1.DataSource = ds.Tables["info_information"];
GridView1.DataBind();
}
//翻页事件处理程序,设置分页控件的当前页索引并重新绑定数据
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
BindData();
}
}