Asp.net paging control, asp.net Paging
Front-end
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Pager.ascx.cs" Inherits="Controls_Core_Pager" %><style type="text/css"> .GridViewPagerStyle { background-position: top; background-repeat: repeat-x; border-bottom: 1px solid #ABC7EC; color: #15428B; font-weight: bold; } .GridViewPagerStyle table { margin: auto; text-align: center; } .GridViewPagerStyle table td { border: 0px; } .GridViewPagerStyle a { color: #15428B; padding: 0px 1px 0px 1px; text-decoration: none; } .GridViewPagerStyle span { color: red; font-weight: bold; padding: 0px 1px 0px 1px; text-decoration: none; }</style><script type="text/javascript" language="javascript"> function checkGo() { var txtGoObj = document.getElementById(document.getElementById('hidTextBoxGoClientID').value); if (txtGoObj && txtGoObj.value == '') { txtGoObj.focus(); return false; } return true; }</script><center> <div>public partial class Controls_Core_Pager : System.Web.UI.UserControl{ public delegate void GridViewDelegate(); #region Delegate public event GridViewDelegate InitLoadData; public event GridViewDelegate PageingLoadData; public event GridViewDelegate BeforePageing; public event GridViewDelegate AfterPageing; private const string firstenabledimgurl = "~/Image/pager/first.gif"; private const string firstdisabledimgurl = "~/image/pager/first_disabled.gif"; private const string lastenabledimgurl = "~/image/pager/last.gif"; private const string lastdisabledimgurl = "~/image/pager/last_disabled.gif"; private const string prevenabledimgurl = "~/image/pager/prev.gif"; private const string prevdisabledimgurl = "~/image/pager/prev_disabled.gif"; private const string nextenabledimgurl = "~/image/pager/next.gif"; private const string nextdisabledimgurl = "~/image/pager/next_disabled.gif"; #endregion public bool SimplePager { set { this.tdAll.Visible = !value; this.tdCurrentIndex.Visible = !value; this.tdImgGo.Visible = !value; this.tdPageCount.Visible = !value; this.tdTextGo.Visible = !value; } } protected void Page_Load(object sender, EventArgs e) { } public int RecordCount { get { return (ViewState["RecordCount"] != null) ? (int)ViewState["RecordCount"] : 0; } set { ViewState["RecordCount"] = value; } } public int PageSize { get { if (string.IsNullOrEmpty(hidPageSize.Value) ||Convert.ToInt32( hidPageSize.Value)<=0) hidPageSize.Value = "20"; return int.Parse(hidPageSize.Value); } set { hidPageSize.Value = value.ToString(); } } public int CurrentPageIndex { get { return (hidCurrentPageIndex.Value == string.Empty) ? 0 : int.Parse(hidCurrentPageIndex.Value); } set { hidCurrentPageIndex.Value = value.ToString(); } } public void LoadData() { if (InitLoadData != null) { InitLoadData(); ShowStatus(0); } } public void ReLoadData() { if (BeforePageing != null) { BeforePageing(); } if (PageingLoadData != null) { PageingLoadData(); ShowStatus(CurrentPageIndex); } if (AfterPageing != null) { AfterPageing(); } } private void ShowStatus(int currentPageIndex) { int pageCount = Convert.ToInt32((RecordCount - 1) / PageSize + 1); lblRecordCount.Text = RecordCount.ToString(); hidCurrentPageIndex.Value = currentPageIndex.ToString(); if (RecordCount == 0) { lblPageCount.Text = "0"; lblCurrentPageIndex.Text = ""; } else { lblPageCount.Text = pageCount.ToString(); lblCurrentPageIndex.Text = Convert.ToString(currentPageIndex + 1); } if (pageCount == 0 || ((currentPageIndex + 1) == 1 && pageCount == 1)) { imgFirstPage.Enabled = false; imgPrePage.Enabled = false; imgNextPage.Enabled = false; imgLastPage.Enabled = false; } else { if (currentPageIndex == 0) { imgPrePage.Enabled = false; imgFirstPage.Enabled = false; imgNextPage.Enabled = true; imgLastPage.Enabled = true; } else if ((currentPageIndex + 1) == pageCount) { imgFirstPage.Enabled = true; imgPrePage.Enabled = true; imgNextPage.Enabled = false; imgLastPage.Enabled = false; } else if (currentPageIndex != 0 && (currentPageIndex + 1) != pageCount) { imgFirstPage.Enabled = true; imgPrePage.Enabled = true; imgNextPage.Enabled = true; imgLastPage.Enabled = true; } } } protected void imgFirstPage_Click(object sender, EventArgs e) { if (BeforePageing != null) { BeforePageing(); } if (PageingLoadData != null) { CurrentPageIndex = 0; PageingLoadData(); ShowStatus(CurrentPageIndex); } if (AfterPageing != null) { AfterPageing(); } } protected void imgPrePage_Click(object sender, EventArgs e) { if (BeforePageing != null) { BeforePageing(); } if (PageingLoadData != null) { CurrentPageIndex = CurrentPageIndex - 1; PageingLoadData(); ShowStatus(CurrentPageIndex); } if (AfterPageing != null) { AfterPageing(); } } protected void imgNextPage_Click(object sender, EventArgs e) { if (BeforePageing != null) { BeforePageing(); } if (PageingLoadData != null) { CurrentPageIndex = CurrentPageIndex + 1; PageingLoadData(); ShowStatus(CurrentPageIndex); } if (AfterPageing != null) { AfterPageing(); } } protected void imgLastPage_Click(object sender, EventArgs e) { if (BeforePageing != null) { BeforePageing(); } if (PageingLoadData != null) { CurrentPageIndex = Convert.ToInt32((RecordCount - 1) / PageSize + 1) - 1; PageingLoadData(); ShowStatus(CurrentPageIndex); } if (AfterPageing != null) { AfterPageing(); } } public void All_Click(object sender, EventArgs e) { if (BeforePageing != null) { BeforePageing(); } if (PageingLoadData != null) { CurrentPageIndex = 0; if (RecordCount > 0) PageSize = Convert.ToInt32(RecordCount); PageingLoadData(); ShowStatus(CurrentPageIndex); } if (AfterPageing != null) { AfterPageing(); } } protected void imgGo_Click(object sender, EventArgs e) { if (BeforePageing != null) { BeforePageing(); } if (PageingLoadData != null) { int pageCount = Convert.ToInt32((RecordCount - 1) / PageSize + 1); int goPageIndex= ConvertToInt(this.txtGo.Text.Trim()); if (pageCount >= goPageIndex) { CurrentPageIndex = goPageIndex - 1; PageingLoadData(); ShowStatus(CurrentPageIndex); } } if (AfterPageing != null) { AfterPageing(); } } private int ConvertToInt(string p) { int refvalue = 0; try { refvalue = int.Parse(p); } catch (Exception) { } return refvalue; } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); this.Page.ClientScript.RegisterHiddenField("hidTextBoxGoClientID", this.txtGo.ClientID); }}
Front-end code on the test page:
<Div> <asp: gridView ID = "GridView1" CssClass = "GridViewStyle" runat = "server" BorderWidth = "0px" CellPadding = "0" AutoGenerateColumns = "False" Width = "100%" AllowSorting = "True "EmptyDataText =" no data "> <FooterStyle CssClass =" GridViewFooterStyle "/> <RowStyle CssClass =" GridViewRowStyle "BackColor =" # F7FAFD "/> <Columns> <asp: boundField DataField = "ID" HeaderText = "ID"> <FooterStyle CssClass = "hide"/> <HeaderStyl E CssClass = "hide"/> <ItemStyle CssClass = "hide"/> </asp: BoundField> <asp: templateField HeaderText = "no."> <ItemTemplate> <% # Container. dataItemIndex + 1%> </ItemTemplate> </asp: TemplateField> <asp: BoundField HeaderText = "ID" DataField = "ID"/> <asp: boundField HeaderText = "PID" DataField = "PID"/> <asp: BoundField HeaderText = "name" DataField = "name"/> <asp: boundField HeaderText = "excavation area" DataField = "GROUPNAME" /> </Columns> <HeaderStyle CssClass = "GridViewHeaderStyle" HorizontalAlign = "Left"/> <AlternatingRowStyle BackColor = "# EBF4FA"/> </asp: gridView> <div> protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {BindGridProject ("select * from student") ;}} private void BindGridProject (string SQL) {ViewState ["SQL"] = SQL; this. uxPager. pageSize = Convert. toInt32 (ConfigurationManager. appSettings ["PageSize"]); this. uxPager. loadData () ;}# region paging related /// <summary> /// initialize the paging control /// </summary> protected void uxPager_InitLoadData () {int totalRecords =-1; dataTable table = QueryTable (this. uxPager. currentPageIndex, this. uxPager. pageSize, ref totalRecords); this. uxPager. recordCount = totalRecords; LoadGridView (table) ;}/// <summary> // load the page control /// </summary> protected void uxPager_PageingLoadData () {int totalRecord = this. uxPager. recordCount; DataTable table = QueryTable (this. uxPager. currentPageIndex, this. uxPager. pageSize, ref totalRecord); LoadGridView (table );} /// <summary> /// obtain the data source /// </summary> /// <param name = "pageIndex"> </param> /// <param name = "pageSize"> </param> // <param name = "totalRecord"> total number of pages </param> /// <returns> </returns> public DataTable QueryTable (int pageIndex, int pageSize, ref int totalRecord) {OracleDataBase odb = new OracleDataBase ("0"); string SQL = ViewState ["SQL"]. toString (); totalRecord = odb. getDataSet (SQL ). tables [0]. rows. count; string sqlTableName = SQL; string selectSql = StructurePagingSql (sqlTableName, pageIndex, pageSize, null); return odb. getDataSet (selectSql ). tables [0];} /// <summary> /// paging statement /// </summary> /// <param name = "strSQL"> </param> /// <param name = "pageIndex"> </param> // <param name = "pageSize"> </param> // <param name = "orderBy"> sort </param>/ // <returns> </returns> public String StructurePagingSql (string sqlTableName, int pageIndex, int pageSize, string orderBy) {int startIndex = pageIndex * pageSize; int endIndex = startIndex + pageSize; StringBuilder selectSql = new StringBuilder (); selectSql. append (string. format ("SELECT * FROM (select. *, rownum rn from ({0}) A ", sqlTableName); selectSql. append (string. format ("where rownum <= {0}) where rn> {1}", endIndex, startIndex); return selectSql. toString ();} private void LoadGridView (DataTable dt) {this. gridView1.DataSource = dt; GridView1.DataBind () ;}# endregion
How to Use the AspNetPager paging Control
Front-end:
<Webdiyer: AspNetPager SubmitButtonClass = "buttons" ID = "AspNetPager1" runat = "server" AlwaysShow = "True" FirstPageText = "Homepage"
NextPageText = "next page" PrevPageText = "Previous Page" LastPageText = "last page" PageSize = "15" ShowInputBox = "Always"
OnPageChanged = "AspNetPager1_PageChanged">
</Webdiyer: AspNetPager>
<! -- PageSize = "15" defines the number of data entries displayed on each page -->
Background:
/// <Summary>
/// Load the event
/// </Summary>
Protected void Page_Load (object sender, EventArgs e)
{
BoundList ();
}
/// <Summary>
/// Global variables, total number of parameter data information used for paging
/// </Summary>
Public static int sumcount;
/// <Summary>
/// Bind information data
/// </Summary>
Private void BoundList ()
{
DataTable dt = GetList (). Tables [0]; // obtain the data source
If (dt. Rows. Count> 0)
{
Sumcount = dt. Rows. Count;
PagedDataSource PPS = new PagedDataSource ();
AspNetPager1.RecordCount = sumcount;
PPS. AllowPaging = true;
PPS. PageSize = AspNetPager1.PageSize;
PPS. CurrentPageIndex = AspN... the remaining full text>
Aspnet custom Page Control
Int pageSize = 4; // number of lines per page
Int nMax = 0; // The total number of records
Int pageCount = 0; // Number of pages = Total number of records/number of lines per page
Int pageCurrent = 0; // current page number
Int nCurrent = 0; // current record row
DataSet ds = new DataSet ();
DataTable dtInfo = new DataTable ();
Private void init ()
{
If (dtInfo. Rows. Count = 0)
{
ToolStripTextBox1.Text = "0 ";
ToolStripLabel3.Text = "/0 ";
TxtCurrentPage. Text = "Total number of pages: 1 ";
LblPageCount. Text = "Total number of pages: 1 ";
}
Else
{
InitDataSet (pageSize );
}
}
Private void InitDataSet (int pages)
{
PageSize = pages; // you can specify the number of pages.
NMax = dtInfo. Rows. Count;
PageCount = (nMax/pageSize); // calculate the total number of pages
If (nMax % pageSize)> 0) pageCount ++;
PageCurrent = 1; // the current page number starts from 1
NCurrent = 0; // The number of current records starts from 0
LoadData ();
}
Private void LoadData ()
{
DataGridView_Area.Refresh ();
Int nStartPos = 0; // records rows starting from the current page
Int nEndPos = 0; // The End record row of the current page
DataTable dtTemp = dtInfo. Clone (); // Clone the DataTable structure framework
If (pageCurrent = pageCount)
NEndPos = nMax;
Else
NEndPos = pageSize * pag ...... remaining full text>