using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class GridView_Page : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //设置分页的图片按钮,这些都可以在控件的属性表上的pagersetting里设置 if (!IsPostBack) { GridView1.Caption = "这是一个GridView的小实验"; //Caption属性类似于表名,显示在控件的正上方。 GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast; GridView1.PagerSettings.NextPageImageUrl = "img/next.gif"; GridView1.PagerSettings.PreviousPageImageUrl = "img/pre.gif"; GridView1.PagerSettings.FirstPageImageUrl = "img/first.gif"; GridView1.PagerSettings.LastPageImageUrl = "img/last.gif"; GridView1.PageSize = 10; //每页最多显示10条记录; BindData(); } } private void BindData() { //将数据部署到GridView中 string Constr = "server=localhost; uid=sa;pwd=123456;database=NorthWind"; string sqlstr = "select * from products"; SqlConnection con = new SqlConnection(Constr); SqlDataAdapter ad = new SqlDataAdapter(sqlstr, con); DataSet ds = new DataSet(); ad.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void GridView1_PageIndexChanged(object sender, EventArgs e) { //进行分页之后,重新部署数据 BindData(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { //分页完成之前 GridView1.PageIndex = e.NewPageIndex; } protected void GridView1_DataBound(object sender, EventArgs e) { //添加分页码显示 GridViewRow bottomPagerRow = GridView1.BottomPagerRow; Label bottomLabel = new Label(); bottomLabel.Text = "目前所在分页:(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")"; bottomPagerRow.Cells[0].Controls.Add(bottomLabel); } }