標籤:style blog class code java tar
分頁詳解,ASP.NET書本 “資料空間應用”
(1)使用GridView控制項分頁顯示 步奏:1.添加GridView控制項 2.將GrideView控制項的AllowPaging屬性設定為True,表示允許分頁 3.將PageSize屬性設定一個數字,用來控制每頁中顯示的記錄數 4.最後在GridView控制項的PageIndexChanging事件中設定GridView控制項的PageIndex屬性為當前頁的索引值,並重新綁定GridView控制項
(2)DataList控制項中的資料
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind();//資料繫結 } } //進行資料繫結的方法 public void Bind() { int CurrentPage = Convert.ToInt32(labNowPage.Text); ///labNowPage標籤 記錄當前頁碼 PagedDataSource ps = new PagedDataSource();//產生PagedDataSource的執行個體 //執行個體化SqlConnection對象 SqlConnection sqlCon = new SqlConnection(); //執行個體化SqlConnection對象串連資料庫的字串 sqlCon.ConnectionString = "server=.;uid=sa;pwd=;database=db_09"; //定義SQL語句 string SqlStr = "select * from tb_Student"; //執行個體化SqlDataAdapter對象 SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon); //執行個體化資料集DataSet DataSet ds = new DataSet(); da.Fill(ds, "tb_Student"); ps.DataSource = ds.Tables["tb_Student"].DefaultView; ps.AllowPaging = true; //是否可以分頁 ps.PageSize = 2; //顯示的數量 ps.CurrentPageIndex = CurrentPage - 1; //取得當前頁的頁碼 lnkbtnFront.Enabled = true; lnkbtnFirst.Enabled = true; lnkbtnNext.Enabled = true; lnkbtnLast.Enabled = true; if (CurrentPage == 1) { lnkbtnFirst.Enabled = false;//不顯示第一頁按鈕 lnkbtnFront.Enabled = false;//不顯示上一頁按鈕 } if (CurrentPage == ps.PageCount) { lnkbtnNext.Enabled = false;//不顯示下一頁 lnkbtnLast.Enabled = false;//不顯示最後一頁 } this.labCount.Text = Convert.ToString(ps.PageCount);//記錄總頁數 this.DataList1.DataSource = ps; this.DataList1.DataKeyField = "ID"; this.DataList1.DataBind(); } //首頁 protected void lnkbtnFirst_Click(object sender, EventArgs e) { this.labNowPage.Text = "1"; this.Bind(); } //上一頁 protected void lnkbtnFront_Click(object sender, EventArgs e) { this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) - 1); this.Bind(); } //下一頁 protected void lnkbtnNext_Click(object sender, EventArgs e) { this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) + 1); this.Bind(); } //尾頁 protected void lnkbtnLast_Click(object sender, EventArgs e) { this.labNowPage.Text = this.labCount.Text; this.Bind(); }}