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
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索gridview
, viewstate
, system
, datasource
, protected
分页下标
,以便于您获取更多的相关知识。
时间: 2024-11-02 07:03:12