asp.net GridView Learning II Custom Paging functionality

Source: Internet
Author: User

1 on some core code

2ClientInfo.cs and ClientinfoAccessObj.cs in the study one.

 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; 16public partial class GridViewPagingTest:System.Web.UI.Page 17{int PageSize = 10;//per page display record number 19 20//Current  Page number, starting from 1, using ViewState to save data between postbacks private int Curpage {returns viewstate["curpage"] = = null?
0:convert.toint32 (viewstate["Curpage"]); {viewstate["curpage"] = value 30} 31} 32 33//Total number of pages, using ViewState to save data between postbacks privat e int PageCount {viewstate["pagecount"] = = null? 0:convert.toint32 (viewstate["Pag
Ecount "]); The set of the viewstate["pagecount"] = value, {} protected void Page_Load (object sender, EventArgs e) (! IsPostBack) 50 {51//First request Curpage = 1; gridview1.datasource = Getclientsforpage (curpage);//According to the current page Get customer Information Lblinfo.text = string.
Format ("{0} page/total {1} page", 1, PageCount); Gridview1.databind ()//Binding Data 56} 57} 58//According to the page subscript get the customer information of the page private list<clientinfo> Getclientsforpag E (int pageIndex) {clientinfoaccessobj accessor = new Clientinfoaccessobj (); List<clientinfo> Client s = accessor. Getallclients ()//Get all customer information PageCount = clients.
Count/pagesize + 1;//The total number of customer information divided by the number of records displayed per page to obtain totals if (PageIndex > PageCount) return null; int StartIndex = (pageIndex-1) * pagesize;//get data subscript list<clientinfo> ret = new list<clientinfo> ()
; for (int i = StartIndex i < StartIndex + PageSize && i < clients. Count; i++) to Ret.
ADD (Clients[i]);
return ret; protected void Btnnext_click (object sender, EventArgs e) () (curpage+1>PageCount)//judge whether the current is greater than the total number of pages curpage = PageCount;
Ridview1.datasource = Getclientsforpage (curpage); Lblinfo.text = string.
Format ("{0} page/total {1} page", Curpage, PageCount);
Gridview1.databind ();
protected void Btnprew_click (object sender, EventArgs e) (= if (curPage-1 ==0)//Determine whether the current is greater than the total number of pages 89 {
Curpage = 1;
The curpage--} is the Gridview1.datasource = Getclientsforpage (curpage); Lblinfo.text = string.
Format ("{0} page/total {1} page", Curpage, PageCount);
Gridview1.databind (); protected void Btngo_click (object sender, EventArgs e)-{102 try-D-int pageIndex = Conv Ert.
ToInt32 (Txtpageindex.text);  Rule if (PageIndex > PageCount) pageIndex = PageCount 108} 109 if (PageIndex <
1) PageIndex = 1 112} 113 curpage = PageIndex; 114 GridView1.
DataSource = Getclientsforpage (curpage); Lblinfo.text = string.
Format ("{0} page/total {1} page", Curpage, PageCount);
116 Gridview1.databind (); 117} 118 catch (Exception ex) 119 {Clientscript.registerclientscriptblock (this. GetType (), "info", "alert (' illegal character ');", true);//inject JavaScript script to page 121} 122} 123} 124

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.