C # ASP. NET implementation Paging (pager) functionality

Source: Internet
Author: User

Paging Pagerhelper Helper Classes

Using System;
Using System.Web;
public class pagerhelper{#region get the HTML code for pagination////<summary>///Get the HTML code for pagination///The current page number method inside according to request["page" ] Get//</summary>//<param name= "PageSize" > Number of pages </param>//<param name= "TotalCount" > Total quantity </param>//<param name= "url" > pseudo-static address such as/news/list-1-{0}.html</param>//<param name= "Maxpagen Um > Maximum number of pages (100 page shows only 8 other hidden) </param>//<returns></returns> public static string getpagehtm L (int pageSize, int totalcount, string url, int maxpagenum = 8) {HttpRequest Request = HttpContext.Current.Requ        Est        int curpageindex = 1; if (!string. Isnullorwhitespace (request["page"]) {Curpageindex = Convert.ToInt32 (request["page"]??            "1"); Curpageindex = Curpageindex <= 0?        1:curpageindex;        } System.Text.StringBuilder pagehtml = new System.Text.StringBuilder (); if (PageIndex > 1)//{pagehtml.append (CurpagEindex = = 1? "<a href=\" javascript:void (0); \ "> Home </a>": "<a href=\" "+ String.        Format (URL, 1) + "\" > Home </a> "); Pagehtml.append (Curpageindex > 1?) "<a href=\" "+ String.        Format (URL, curPageIndex-1) + "\" > Prev </a> ":" <a href=\ "javascript:void (0); \" > Previous </a> "); } int pagecount = Getpagecount (pageSize, totalcount);//Total page number//Get display area first start position as 1 9 int firstnum = Curpageindex% Maxpagenum = = 0?        Curpageindex-(maxPageNum-1): curpageindex-curpageindex% maxpagenum + 1; if (Firstnum > Maxpagenum) {pagehtml.append ("<a href=\" + string.        Format (URL, firstNum-1) + "\" >...</a> ");            } for (int i = firstnum; I < Firstnum + Maxpagenum; i++) {if (i > PageCount) break; String css = string.            Empty;            if (i = = curpageindex) {css = "class=\" currentpage\ ""; } PAGEHTML.APpend ("<a" + CSS + "href=\" + string.        Format (URL, i) + "\" > "+ i +" </a> "); } if (PageCount >= firstnum + maxpagenum) {pagehtml.append ("<a href=\" + string.        Format (URL, firstnum + maxpagenum) + "\" >...</a> "); }//if (PageCount > Curpageindex)//{pagehtml.append (Curpageindex < PageCount?) "<a href=\" "+ String.        Format (URL, Curpageindex + 1) + "\" > Next </a> ":" <a href=\ "javascript:void (0); \" > Next </a> "); Pagehtml.append ("<a href=\" + string.        Format (URL, pagecount) + "\" > End </a> "); } pagehtml.append (String.        Format ("<a href=\" javascript:void (0); \ "> Total {0} pages, {1} strips </a>", PageCount, TotalCount));    return pagehtml.tostring (); #endregion #region Get the total number of pages//<summary>///Get Total page Numbers//</summary>//<param name= "page Size > Quantity per page </param>//<param name= "TotalCount" > Total quantity </param> <returns></returns> private static int getpagecount (int pageSize, int totalcount) {int Pagen        Umbers = 0;        if (totalcount% pageSize! = 0) {pagenumbers = totalcount/pagesize + 1;        } else {pagenumbers = totalcount/pagesize;        } pagenumbers = Convert.ToInt32 (math.ceiling (convert.todouble (totalcount)/convert.todouble (pageSize)));    return pagenumbers; } #endregion}

Foreground use

<%@ page language= "C #" autoeventwireup= "true"%><! DOCTYPE html>

Preview Map

C # ASP. NET implementation Paging (pager) functionality

Related Article

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.