GridPager.ascx:
<% @ Control Language = "c #" AutoEventWireup = "false" Codebehind = "GridPager.ascx.cs" Inherits = "Test.BaseClass.GridPager" TargetSchema = "http://schemas.microsoft.com/intellisense/ie5"% > xml: namespace prefix = ASP /> asp: linkbutton> 1 ask: linkbutton> ask: linkbutton> ask: linkbutton> < / asp: linkbutton> asp : LinkButton> ask: linkbutton> asp: linkbutton> ask: linkbutton> asp: linkbutton> ask: linkbutton> asp: linkbutton> ///
GridPager.ascx.cs:
Using system;
Using system.data;
Using system.drawing;
Using system.Web;
Using system.Web.ui.webcontrols;
Namespace Test.baseclass
{
Public Class GridPager: System.Web.ui.userControl
{
#REGION class variable
Protected system.web.ui.webcontrols.linkButton lbtnnext;
Protected system.Web.ui.webcontrols.linkButton lbtnpage10;
Protected system.Web.ui.WebControls.LinkButton lbtnpage9;
Protected system.Web.ui.WebControls.LinkButton lbtnpage8;
Protected system.Web.ui.WebControls.LinkButton lbtnpage7; protected system.web.ui.webcontrols.linkButton lbtnpage6;
Protected system.Web.ui.WebControls.LinkButton lbtnpage5;
Protected system.Web.ui.WebControls.LinkButton lbtnpage4;
Protected system.Web.ui.WebControls.LinkButton lbtnpage3;
Protected system.web.ui.webcontrols.linkButton lbtnpage2;
Protected system.web.ui.webcontrols.linkbutton lbtnpage1;
Protected system.web.ui.webcontrols.linkButton lbtnpre;
#ndregion
#REGION
///
/// No .: 01
/ // Content Summary: Turn the object: DataGrid control.
/// summary>
Public DataGrid thegrid;
/// /// Number: 02 /// Content Summary: Display the label of the query result record number, may not be set, indicating that there is no need to display. /// summary> public label labelrowscount;
/// /// Number: 03 /// Content Summary: The label of the total number of query results is displayed, which may not be set, indicating that there is no need to display. /// summary> public label labelpagecount;
/// /// Number: 04 /// Content Summary: Displays the label of the current page number, may not be set, indicating that there is no need to display. /// summary> public label labelpageindex;
/// /// No .: 05 /// Content Summary: Query data SQL statement. . /// summary> private string SelectSQL {get {if (! ViewState [this.ClientID "_ SelectSQL"] = null) return ViewState [this.ClientID "_ SelectSQL"] ToString (); else return string.Empty;} Set {ViewState [this.clientID "_ selectsql"] = value;}} // selectsql end
/// /// No .: 06 /// Content Summary: The number of rows shown per page. /// summary> public int pageSizes {get {if (viewState [this.clientid "_ pagesizes"]! = null) return (int) viewState [this.clientid "_ pagesizes"]; else return 20;} set {viewState [this.clientID "_ pagesizes"] = value;}} // Pagesizes end /// /// No .: 07 /// Content Summary: The total number of pages. /// summary> private int PageCount {get {if (! ViewState [this.ClientID "_ PageCount"] = null) return (int) ViewState [this.ClientID "_ PageCount"]; else return 0;} set {ViewState [this.clientid "_ pageCount"] = value;}} // pageCount end
/// /// No .: 08 /// Content Summary: Current page number. /// summary> private int CurrentPageIndex {get {if (ViewState [this.ClientID "_ CurrentPageIndex"] = null!) Return (int) ViewState [this.ClientID "_ CurrentPageIndex"]; else return 0;} set {ViewState [this.ClientID "_ CurrentPageIndex"] = value; if (this.LabelPageIndex = null!) this.LabelPageIndex.Text = (value 1) .ToString ();}} // CurrentPageIndex end private LinkButton [] PageButtons {get { return new LinkButton [] {lbtnPage1, lbtnPage2, lbtnPage3, lbtnPage4, lbtnPage5, lbtnPage6, lbtnPage7, lbtnPage8, lbtnPage9, lbtnPage10};}} # endregion # region method of area begins /// /// method number: 01 / // Content Summary: Initialization Page. /// summary> private void page_load (object sender, system.eventargs e) {}
/// /// method number: 02 /// Content Summary: Set the control associated with this control. /// summary> /// DataGrid control, you must provide param> /// Show the query result record number, you can enter NULL, Indicates that there is no need to display the "LBLPAGECount"> display the number of query results, you can enter NULL, indicating that there is no need to display param> /// display the current page number tag can enter null, meaning without show public void InitPagerControls (DataGrid mDataGrid, label lblRowsCount, label lblPageCount, label lblPageIndex) {mDataGrid.AllowPaging = false; this.TheGrid = mDataGrid; this.LabelRowsCount = lblRowsCount "This.labelpageCount; this.labelpageindex = lblpageindex;} /// /// method number: 03 /// Content summary: Change the query condition to re-query data. /// summary> /// The SQL statement of the query, always use this statement to get the data set until the next call is called param> public void loadData (String SELECTSQL) {// Save new SQL statement this.selectsql = selectsql;
// Statistically eliminate the number of requirements INT MROWCOUNT = 0; String Tmpsql = "STRING TMPSQL =" SELECT Count (*) from (" SelectSql ") T "; Database DB = New Database (); system.data.oledb.oledbDataReader TMPDR = DB.GetdataReaderfromsql (Tmpsql); if (Tmpdr.Read ()) MROWCOUNT = Convert.Toint32 (TMPDR [0]); // Release data connection TMPDR.CLOSE (); db.dispose ();
// If necessary, the number of data is displayed (this.labelrowscount! = Null) this.labelrowscount.text = MROWCOUNT.TOSTRING (); // Statistics Total number, if needed, display THISPAGECOUNT = (int ) Math.ceiling ((Double) MROWCOUNT / ((Double) PageSizes); if (this.labelpagecount! = Null) this.labelpagecount.text = this.pagecount.toString ();
// DataGrid's current page Back to the first page, the button also returns the status of the first page this.currentpageIndex = 0; this.changepageButtons (0);
// Query the first page Data this.lbtnpages_click (this.pagebuttons [0], null);
}
/// /// method number: 04 /// Content Summary: Query the requirements of the qualified data and bind DataGrid display. /// summary> private void binddata () {string selectsql = this.selectsql;
IF (SelectSql! = String.empty) {database DB = new database (); // This is my own data operation class
INT rowstart = (currentPageIndex * pagesizes) 1; // Starter number INT Rownd = (CurrentPageIndex 1) * Pagesizes; // End serial number of the current page
String querysql = "SELECT * FROM (SELECT T1. *, ROWNUM SN from (" SELECTSQL ") T1) T2" "Where t2.sn between" rowstart.tostring () "and" rowend.toString ( ); / / Query the database read the eligible data DATATABLE TB = DB.GetDataTableFromSQL (QuerySQL);
// Bind DataGrid display thegrid.datasource = Tb; THEGRID.DATABIND ();
db.dispose ();}}
/// /// method number: 05 /// Content summary: Change the state of the flip page button, such as display 21, 22, ... 30. /// summary> Private void callbuttons (int mark) {int TmpRem = 0; int Tmpdiv = Math.divrem (this.pagecount, 10, out tmpRem); if (tmpRem == 0) TMPREM = 10; // All buttons restore the default status for (int i = 0; i <10; i ) {int Pagenum = mindex * 10 i 1; this.pageButtons [i] .text = Pagenum.toString (); this.pageButtons [ I] .visible = true; this.pageButtons [i] .enabled = true; this.pageButtons [i] .font.size = system.Web.ui.WebControls.FontUnit.xsmall;} // end of for (int i = 0; i <10; i )
This.lbtnpre.visible = true; this.lbtnnext.visible = TRUE;
// If you are currently from 1 to 10 pages, then the front "..." if (mindex == 0) lbtnpre.visible = false; // If it is currently the last page, do not show "" ... "; and guarantee that the displayed flip button does not exceed the maximum number of pages if (MINDEX == Tmpdiv) {lbtnnext.visible = false; for (int i = 9; i> tmpRem-1; i = i-1) this.pageButtons [i] .visible = false;}}
/// /// method number: 06 /// Content Summary: Show the top ten page number. /// summary> private void lbtnpre_click (object sender, system.eventargs e) {this.changepageButtons ((int) Math.floor (Math.max (this.currentpageIndex - 10, 0) / 10)); LBTNPAGES_CLICK (this.pageButtons [9], null);
/// /// method number: 07 /// Content Summary: The last ten page numbers are displayed. /// summary> private void lbtnnext_click (object sender, system.eventargs e) {this.changepagebuttons ((int) Math.floor (Math.max (this.currentpageIndex 10); this. LBTNPAGES_CLICK (this.pageButtons [0], null);} /// /// method number: 08 /// Content Summary: Query the new page. /// summary> private void lbtnPages_Click (object sender, System.EventArgs e) {LinkButton tmpLBtn = sender as LinkButton; int pageIndex = Convert.ToInt32 (tmpLBtn.Text.Trim ()) - 1; this.CurrentPageIndex = pageIndex ;
Foreach (LinkButton Mlbtn in this.pageButtons) {mlbtn.enabled = true; mlbtn.font.size = system.web.ui.webcontrols.fontunit.xsmall;
TMPLBTN.ENABLED = false; tmplbtn.font.size = system.web.ui.webcontrols.fontunit.small;
THIS.BINDDATA ();
#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // initializeComponent (); base.oninit (e);} /// /// Designer Support required method - Do not use code editor // to modify the contents of this method. /// summary> private void InitializeComponent () {this.lbtnPre.Click = new System.EventHandler (this.lbtnPre_Click); this.lbtnPage1.Click = new System.EventHandler (this.lbtnPages_Click); this.lbtnPage2 .Click = new System.EventHandler (this.lbtnPages_Click); this.lbtnPage3.Click = new System.EventHandler (this.lbtnPages_Click); this.lbtnPage4.Click = new System.EventHandler (this.lbtnPages_Click); this. lbtnPage5.Click = new System.EventHandler (this.lbtnPages_Click); this.lbtnPage6.Click = new System.EventHandler (this.lbtnPages_Click); this.lbtnPage7.Click = new System.EventHandler (this.lbtnPages_Click); this .lbtnPage8.Click = new System.EventHandler (this.lbtnPages_Click); this.lbtnPage9.Click = new System.EventHandler (this.lbtnPages_Click); this.lbtnPage10.Click = new System.EventHandler (this.lbtnPages_Click); THIS.LBTNNEXT.Click = New System. EventHandler (this.lbtnnext_click; this.load = new system.eventhandler (this.page_load);} #ENDREGION
#ndregion