1就一些核心代碼
2ClientInfo.cs和ClientinfoAccessObj.cs在學習一中有過了
3 4using System; 5using System.Data; 6using System.Configuration; 7using System.Collections; 8using System.Web; 9using System.Web.Security; 10using System.Web.UI; 11using System.Web.UI.WebControls; 12using System.Web.UI.WebControls.WebParts; 13using System.Web.UI.HtmlControls; 14using System.Collections.Generic; 15 16public partial class GridViewPagingTest : System.Web.UI.Page 17{ 18 private int PageSize = 10; //每頁顯示記錄數 19 20 //當前頁碼,從1開始,利用ViewState在回發之間保存數據 21 private int curPage 22 { 23 get 24 { 25 return ViewState["curPage"] == null ? 0 : Convert.ToInt32(ViewState["curPage"]); 26 } 27 set 28 { 29 ViewState["curPage"] = value; 30 } 31 } 32 33 //總頁數,利用ViewState在回發之間保存數據 34 private int PageCount 35 { 36 get 37 { 38 return ViewState["PageCount"] == null ? 0 : Convert.ToInt32(ViewState["PageCount"]); 39 } 40 set 41 { 42 ViewState["PageCount"] = value; 43 } 44 } 45 46 47 protected void Page_Load(object sender, EventArgs e) 48 { 49 if (!IsPostBack) 50 { 51 //第一次請求 52 curPage = 1; 53 GridView1.DataSource = GetClientsForPage(curPage);//根據當前頁獲得客戶信息 54 lblInfo.Text = string.Format("第{0}頁/共{1}頁", 1, PageCount); 55 GridView1.DataBind();//綁定數據 56 } 57 } 58 //根據頁下標獲得頁面的客戶信息 59 private List<ClientInfo> GetClientsForPage(int pageIndex) 60 { 61 ClientInfoAccessObj accessor = new ClientInfoAccessObj(); 62 List<ClientInfo> clients = accessor.GetAllClients();//獲得所有客戶信息 63 PageCount = clients.Count / PageSize + 1;//將客戶信息的總數除以每頁顯示的記錄數獲得總頁數 64 if (pageIndex > PageCount) 65 return null; 66 int StartIndex = (pageIndex - 1) * PageSize;//獲得數據下標 67 List<ClientInfo> ret = new List<ClientInfo>(); 68 for (int i = StartIndex; i < StartIndex + PageSize && i < clients.Count; i++) 69 ret.Add(clients[i]); 70 return ret; 71 } 72 protected void btnNext_Click(object sender, EventArgs e) 73 { 74 if (curPage+1>PageCount)//判斷當前是否大於頁總數 75 { 76 curPage = PageCount; 77 } 78 else 79 { 80 curPage++; 81 } 82 GridView1.DataSource = GetClientsForPage(curPage); 83 lblInfo.Text = string.Format("第{0}頁/共{1}頁", curPage, PageCount); 84 GridView1.DataBind(); 85 } 86 protected void btnPrew_Click(object sender, EventArgs e) 87 { 88 if (curPage - 1 ==0 )//判斷當前是否大於頁總數 89 { 90 curPage = 1; 91 } 92 else 93 { 94 curPage--; 95 } 96 GridView1.DataSource = GetClientsForPage(curPage); 97 lblInfo.Text = string.Format("第{0}頁/共{1}頁", curPage, PageCount); 98 GridView1.DataBind(); 99 } 100 protected void btnGo_Click(object sender, EventArgs e) 101 { 102 try 103 { 104 int pageIndex = Convert.ToInt32(txtPageIndex.Text); 105 if (pageIndex > PageCount) 106 { 107 pageIndex = PageCount; 108 } 109 if (pageIndex < 1) 110 { 111 pageIndex = 1; 112 } 113 curPage = pageIndex; 114 GridView1.DataSource = GetClientsForPage(curPage); 115 lblInfo.Text = string.Format("第{0}頁/共{1}頁", curPage, PageCount); 116 GridView1.DataBind(); 117 } 118 catch (Exception ex) 119 { 120 ClientScript.RegisterClientScriptBlock(this.GetType(),"info","alert('非法字符');",true);//向頁面注入javaScript腳本 121 } 122 } 123} 124