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 id = lbtnpre style = "border-Right: White 2px solid; border-left: white 1px solid "runat =" server "text = "... "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage1 style =" border-Right: White 2px solid; border-left: white 1px solid "runat =" server "text =" 1 "visible =" false "enabled =" false "font-size =" small "> 1 </ASP: linkbutton> <asp: linkbutton id = lbtnpage2 style = "border-Right: White 2px solid; border-left: white 1px solid "runat =" server "text =" 2 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage3 style =" border-right: white 2px solid; border-left: White 1px solid "runat =" server "text =" 3 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage4 style = "border-Right: White 2px solid; border-left: white 1px solid "runat =" server "text =" 4 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage5 style =" border-right: white 2px solid; border-left: White 1px solid "runat =" server "text =" 5 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage6 style = "border-Right: White 2px solid; border-left: white 1px solid "runat =" server "text =" 6 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage7 style =" border-right: white 2px solid; border-left: White 1px solid "runat =" server "text =" 7 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage8 style = "border-Right: White 2px solid; border-left: white 1px solid "runat =" server "text =" 8 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage9 style =" border-right: white 2px solid; border-left: White 1px solid "runat =" server "text =" 9 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnpage10 style = "border-Right: White 2px solid; border-left: white 1px solid "runat =" server "text =" 10 "visible =" false "> </ASP: linkbutton> <asp: linkbutton id = lbtnnext style =" border-right: white 2px solid; border-left: White 1px solid "runat =" server "text = "... "visible =" false "> </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 Variables
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;
# Endregion
# Region
/// <Summary>
/// ID: 01
/// Content summary: the object to flip pages: DataGrid Control.
/// </Summary>
Public DataGrid thegrid;
/// <Summary>
/// No.: 02
/// Content summary: The tag that displays the number of records in the query result. If this parameter is not set, no display is required.
/// </Summary>
Public label labelrowscount;
/// <Summary>
/// No.: 03
/// Content summary: The tag that displays the total number of pages of the query result. If this parameter is not set, no display is required.
/// </Summary>
Public label labelpagecount;
/// <Summary>
/// No.: 04
/// Content summary: displays the label of the current page number. It can be left blank, indicating no display is required.
/// </Summary>
Public label labelpageindex;
# The region method area starts.
/// <Summary>
/// Method no.: 01
/// Content summary: initialization page.
/// </Summary>
Private void page_load (Object sender, system. eventargs E)
{
}
/// <Summary>
/// Method no.: 02
/// Content summary: Set the control associated with this control.
/// </Summary>
/// <Param name = "mdatagrid"> DataGrid Control, required </param>
/// <Param name = "lblrowscount"> the label that displays the number of records in the query result. You can enter null, indicating that no display is required. </param>
/// <Param name = "lblpagecount"> label that displays the total number of pages in the query result. You can enter null, indicating that no display is required. </param>
/// <Param name = "lblpageindex"> display the label of the current page number. You can enter null, indicating that no display is required. </param>
Public void initpagercontrols (DataGrid mdatagrid, label lblrowscount, label lblpagecount, label lblpageindex)
{
Mdatagrid. allowpaging = false;
This. thegrid = mdatagrid;
This. labelrowscount = lblrowscount;
This. labelpagecount = lblpagecount;
This. labelpageindex = lblpageindex;
}
/// <Summary>
/// Method no.: 03
/// Content summary: Modify the query conditions and then query the data again.
/// </Summary>
/// <Param name = "selectsql"> when querying an SQL statement, always use this statement to retrieve the dataset until the next call to this method </param>
Public void loaddata (string selectsql)
{
// Save the new SQL statement
This. selectsql = selectsql;
// Count the number of data entries that meet the condition
Int mrowcount = 0;
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 the data connection
Tmpdr. Close ();
DB. Dispose ();
// Display the total number of data rows if needed
If (this. labelrowscount! = NULL)
This. labelrowscount. Text = mrowcount. tostring ();
// Count the total number of pages. If necessary, the total number of pages is displayed.
This. pagecount = (INT) math. Ceiling (double) mrowcount)/(double) pagesizes ));
If (this. labelpagecount! = NULL)
This. labelpagecount. Text = This. pagecount. tostring ();
// The current page of The DataGrid returns to the first page, and the button also returns the status of the first page
This. currentpageindex = 0;
This. changepagebuttons (0 );
// Query the first page of data
This. lbtnpages_click (this. pagebuttons [0], null );
}
/// <Summary>
/// Method no.: 04
/// Content summary: query the Qualified Data and bind it to the DataGrid.
/// </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; // start Number of the current page
Int rowend = (currentpageindex + 1) * pagesizes; // end 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 to read Qualified Data
Datatable TB = dB. getableablefromsql (querysql );
// Bind the DataGrid to display
Thegrid. datasource = Tb;
Thegrid. databind ();
DB. Dispose ();
}
}
/// <Summary>
/// Method no.: 05
/// Content summary: changes the page flip button status, for example, the page numbers 21, 22, and... 30 are displayed.
/// </Summary>
Private void changepagebuttons (INT mindex)
{
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 ++)
// If it is 1 ~ 10 pages, so do not display the "..." in front of 1
If (mindex = 0)
Lbtnpre. Visible = false;
// If the current page is the last one, the "..." button is not displayed, and the page turning button cannot 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;
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor
/// Modify the content 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 );
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