asp: literal> page
asp: literal> page per page
asp: literal>
asp: literal>
Td>
TR>
Table> form>
body>
Html>
Backstage CS file code, DataGridPaging class inherits from system.web.ui.page, pay attention to no data when data binding (0 pages), and avoiding the front desk in the back page when the number of pages is reduced.
Using system;
Using system.collections;
Using system.componentmodel;
Using system.data;
Using system.drawing;
Using system.Web;
Using system.Web.SessionState;
Using system.Web.ui;
Using system.Web.ui.webcontrols;
Using system.Web.ui.htmlcontrols;
Using system.data.sqlclient;
Using system.configuration;
Namespace zz.aspnetPaging
{
Public Class DataGridPaging: System.Web.ui.page
{
Private static string connString = configurationSettings.appsettings ["connString"];
PRIVATE INT RecordCount;
PRIVATE INT pageCOUNT;
Protected system.Web.ui.WebControls.LinkButton lbtnfirst;
Protected system.Web.ui.WebControls.LinkButton lbtnprev;
Protected system.web.ui.webcontrols.linkButton lbtnnext;
Protected system.Web.ui.WebControls.LinkButton lbtnlast;
protected system.web.ui.webcontrols.literal ltlpageindex;
Protected system.web.ui.webcontrols.literal ltlpagecount;
Protected system.web.ui.webcontrols.literal ltlpagesize;
Protected system.Web.ui.WebControls.litral ltlrecordcount;
Protected system.web.ui.webcontrols.dataGrid DataGrid1;
Private Void Page_Load (Object Sender, System.EventArgs E)
{
IF (! page.ispostback)
{
DataGriddatabind ();
}
}
// Binding data
Private void DataGridDataBind ()
{
DataSet DS = getCustomersData ();
Recordcount = ds.tables [0] .rows.count;
/ / Get the current number of pages
PageCount = (int) Math.ceiling (RecordCount * 1.0 / Pagesize);
// Avoid record from time to time, and has been reversed, CURRENTPAGEINDEX> PAGECOUNT error
IF (RecordCount == 0)
{
this.dataGrid1.currentpageIndex = 0;
}
Else if (this.DataGrid1.currentpageindex> = PageCount) {
THIS.DATAGRID1.CURRENTPAGEINDEX = PAGECUNT - 1;
}
THIS.DATAGRID1.DATASOURCE = DS;
THIS.DATAGRID1.DATABIND ();
NavigationStateChange ();
}
#Region web form designer generated code
Override protected void oninit (Eventargs E)
{
//
// Codegen: This call is necessary for the ASP.NET Web Form Designer.
//
InitializationComponent ();
Base.onit (e);
}
///
/// Designer supports the required method - do not use the code editor to modify
/// This method is content.
/// summary>
Private vidinitiRizeComponent ()
{
THIS.LBTNFIRST.Click = new system.eventhandler (this.lbtnavigation_click);
THIS.LBTNPREV.Click = new system.EventHandler (this.lbtnavigation_click);
THIS.LBTNNEXT.Click = new system.eventhandler (this.lbtnnavigation_click);
THIS.LBTNLAST.CLICK = New System.EventHandler (this.lbtnnaVigation_click);
This.Load = New System.EventHandler (this.page_load);
}
#ndregion
Private void lbtnnavigation_click (Object Sender, System.Eventargs E)
{
LinkButton BTN = (LinkButton) Sender;
Switch (btn.commandname)
{
Case "first":
PageIndex = 0;
Break;
CASE "prev": // if (pageindex> 0)
PageIndex = PageIndex - 1;
Break;
Case "next": // if (pageIndex
PageIndex = PageIndex 1;
Break;
Case "Last":
PageIndex = PageCount - 1;
Break;
}
DataGriddatabind ();
}
// data binding
Public Static Dataset getCustomersData ()
{
SqlConnection conn = new sqlconnection (connString);
String Sqlstr = "Select Customerid, CompanyName, Address, Phone from Customers";
SQLCommand Comm = New Sqlcommand (SQLSTR, CONN);
SqlDataAdapter DataAdapter = New SqlDataAdapter (Comm);
DataSet DS = New Dataset ();
DataAptapter.Fill (DS); Return DS;
}
///
// / Control the status of the navigation button or number
/// summary>
Public void navigationStateChange ()
{
IF (PageCount <= 1) // (RecordCount <= PageSize) / / less than or equal to one page
{
THIS.LBTNFIRST.ENABLED = FALSE;
this.lbtnprev.enabled = false;
THIS.LBTNNEXT.ENABLED = FALSE;
THIS.LBTNLAST.ENABLED = FALSE;
}
Else / / Multi-page
{
IF (pageIndex == 0) // currently the first page
{
THIS.LBTNFIRST.ENABLED = FALSE;
this.lbtnprev.enabled = false;
THIS.LBTNNEXT.ENABLED = True;
THIS.LBTNLAST.ENABLED = True;
}
Else IF (PageIndex == PageCount - 1) // Currently for the last page
{
THIS.LBTNFIRST.ENABLED = TRUE;
THIS.LBTNPREV.ENABLED = TRUE;
THIS.LBTNNEXT.ENABLED = FALSE;
THIS.LBTNLAST.ENABLED = FALSE;
}
ELSE // Intermediate Page
{
THIS.LBTNFIRST.ENABLED = TRUE;
THIS.LBTNPREV.ENABLED = TRUE;
THIS.LBTNNEXT.ENABLED = True;
THIS.LBTNLAST.ENABLED = True;
}
}
IF (RecordCount == 0) // DataGrid.pageCount will display 1 page when there is no record
THIS.LTLPAGECOUNT.TEXT = "0";
Else
THIS.LTLPAGECOUNT.TEXT = PageCount.toString ();
IF (RecordCount == 0)
this.ltlpageIndex.text = "0";
Else
THIS.LTLPAGEINDEX.TEXT = (PageIndex 1) .tostring (); // In the case of the number of pages, the front display page number plus 1
THIS.LTLPAGESIZE.TEXT = PAGESIZE.TOSTRING ();
THIS.LTLRECORDCOUNT.TEXT = RecordCount.toString ();
}
// total pages
Public int pageCount
{
Get {return this.datagrid1.pagecount;
}
// page size
Public Int PageSize
{
Get {return this.datagrid1.pagesize;
}
// Page Index, starting from scratch
Public int pageIndex
{
Get {return this.datagrid1.currentpageindex;}
Set {this.dataGrid1.currentpageindex = value;
}
// Total number of records
Public int Recordcount
{
Get {return.
Set {recordcount = value;}
}
}
}
转载请注明原文地址:https://www.9cbs.com/read-107957.html