asp.net (C #) complete solution based on stored procedure paging

Source: Internet
Author: User

Put the effect chart first:

The specific process is as follows:

1. Paging Stored Procedures

(@Tables varchar (1000) = ',--table name or cascading name @PrimaryKey varchar (100) = ',--primary Key name @Sort varchar (200) = ',--row sequence, including Asc/desc @CurrentPage int = 1,--The current page number @PageSize int = 10,--Displays the number of records per page @Fields varchar (1000) = ',----the list of fields read @Filter varchar (1000) = ' ',--Query conditions @Group varchar (1000) = '--aggregate query information] AS/* Store query String * * DECLARE @strSQL nvarchar (2048) Set @strSQL = ' * Store query condition * * DECLARE @strFilters nvarchar (1000) Set @strFilters = '/* store sort field/DECLARE @strSort nvarchar (+) Set @strSort = ' ' * * Storage aggregation query/DECLARE @strGroup nvarchar (1100) Set @strGroup = ' if (@Filter is not NULL and @Filter!= ') BEGIN if ( @CurrentPage = 1) Set @strFilters = ' WHERE (' + @Filter + ') ' ELSE set @strFilters = ' (' + @Filter + ') ' End if ( @Sort is not null and @Sort!= ' ") BEGIN set @strSort = ' ORDER BY ' + @Sort End if (@Group be not null and @Group!= ' The begin set @strGroup = ' Group by ' + @Group end ELSE BEGIN Set @strGroup = ' End * * Correct query field information/if (@Fields is NULL OR @Fields = ') Set @Fields = ' * '/* Store the number of records fetched per page/DECLARE @strTop nvarchar () Set @strTOP = Convert (nvarchar (4), @PageSize) print @strTOP Prin T @strGroup/* If it is the first page, direct extraction of/if (@CurrentPage = 1) BEGIN Set @strSQL = ' SELECT top ' + @strTop + ' + @Fields + ' from ' + @Tables + @strFilters + @strSort + @strGroup end ELSE BEGIN Set @strSQL = ' SELECT top ' + convert (nvarchar (4), @PageSize + ' + @Fields + ' from ' + @Tables + ' WHERE ' + @strFilters + ' and (' + @PrimaryKey + ' is not in (SELECT top ' + Con Vert (nvarchar (4), ((@CurrentPage-1) * @PageSize)) + ' + @PrimaryKey + ' from ' + @Tables + ' WHERE ' + @strFilter s + @strSort + ')] ' + @strSort end print @strSQL/* Execute query/EXEC sp_executesql @strSQL

2. Pagination C # source code, note: to inherit the control class, you can see the annotation.

public class C_userpage:control {///<summary>///set the number of bars displayed per page///</summary> public int Eachpageitemcoun t = 0; <summary>///Set Current page///</summary> public int currentpage = 0; <summary>///Set Total entry number///</summary> public int ItemCount = 0; <summary>///The total number of pages after paging///</summary> private int Allpagecount {get; set;}///<summary>///set first Page///</summary> private int FirstPage {get {1;}}///<summary>///set last page///</summary> priv ate int LastPage {get {return allpagecount}}///<summary>///set prev///</summary> private int BackPage {get {return CurrentPage < 2 1: (CurrentPage-1);}} <summary>///Set next page///</summary> private int NextPage {get {return currentpage = = Allpagecount? Allpagecount:currentpage + 1; } private int _pagenumber; <summary>///Set the number of digits displayed on each page must be greater than 0 and an even///</summary> public int PageNumber {get {if (_pageNumber < 2) {return 0;} else if (_pagenumber% 2!= 0) {return _pagenumber-1;} else {return _pagenumber;}} set {_pagenumber = value;}} private bool _indexandlaststate = true; <summary>///Display the default display///</summary> public bool Indexandlaststate {get {return _indexandlaststate; set {_indexandlaststate = value;}} private bool _numberstate = true; <summary>///Display digital pagination default display///</summary> public bool Numberstate {get {return _numberstate;} set {_n Umberstate = value; } private bool _pageinfostate = true; <summary>///whether to display paging information (such as number of pages)///</summary> public bool Pageinfostate {get {return _pageinfostate;} s ET {_pageinfostate = value;}} <summary>///Set the URL of the current page///</summary> public string pageurl = ""; <summary>///Rewrite Render method writes required information to HTML control///</summary>///<param name= "Writer" ></param> protected override void Render (HtmlTextWriter writer) {if (ItemCount > EAchpageitemcount) {allpagecount = (ItemCount% eachpageitemcount) < 1? Itemcount/eachpageitemcount: (itemcount/eachpageitemcount) + 1;//get total page number} else {allpagecount = 1;} int p = 0;/* page number (1 2 3 4 5 ...) */String strText = String. Empty; for (int i = 1; i < pagenumber i++) {if (CurrentPage <= (ALLPAGECOUNT-PAGENUMBER/2)) {if (CurrentPage >= P AGENUMBER/2) {p = (currentpage + i)-PAGENUMBER/2} else/* defines the number of page numbers that are also displayed when the current page is less than the number per page/2/{int j = CurrentPage; p = CurrentPage + i-j; j + +; } else {Int j =-1; p = Allpagecount-pagenumber + i-j; j + +;} if (P > 0 && P <= allpagecount) {if P = = CurrentPage) {StrText + + <strong> + p + ' </strong> ';} else {strText = "<a href="/"mce_href="/"" + Pageurl + p + "/" > "+ P +" </a> "; }} StringBuilder sb = new StringBuilder (); if (indexandlaststate) {sb. Append ("<a href="/"mce_href="/"" "+ Pageurl + firstpage +"/"class=/" songti/"><< home </a>"); } sb. AppEnd ("<a href="/"mce_href="/"" "+ Pageurl + BackPage +"/"class=/" songti/">< prev </a>"); if (numberstate) {sb. Append (StrText); } sb. Append ("<a href="/"mce_href="/"" "+ Pageurl + NextPage +"/"class=/" songti/"> Next page ></a>"); if (indexandlaststate) {sb. Append ("<a href="/"mce_href="/"" "+ Pageurl + lastpage +"/"class=/" songti/"> Last >></a>"); } if (pageinfostate) {sb. Append ("<span> Total" + allpagecount + "page" + ItemCount + "message </span>");} Writer. Write (SB); Base. Render (writer); } }

3. The URL passing parameters of the previous page should be transmitted in this way;

<asp:hyperlink id= "Hlink_fircatalogid" runat= "server" Navigateurl= ' <%# ' ~/ivxworksportal/portal/ Showalllist.aspx?fircatalogid= "+ DataBinder.Eval (Container.DataItem," f_fircatalogid ") +" &page=1 "%> ' Text= ' <%# DataBinder.Eval (Container.DataItem, "F_fircatalogname")%> ' ></asp:HyperLink>

4. Front Desk: Add the following code at the top of the. aspx page that requires pagination.

<%@ Register assembly= "Ivxworkssys" namespace= "IVxWorksSYS.IVxWorksCode.PublicClass" tagprefix= "Page"%>

5. Front Desk: then reference control;

<div class= "userpage" > <page:c_userpage id= "MyPage" runat= "Server" > </Page:C_UserPage> </div >

6. Background code as follows, and pagination related to 5 parameters, may have to make corresponding changes. where the Bind_alllistbyfircatalogid () function is obtained from the background data, no longer write.

IVxWorksCode.IVxWorksPortal.C_Articles_BLL C_art = new IVxWorksCode.IVxWorksPortal.C_Articles_BLL (); String fircatalogid = request.querystring["Fircatalogid"]. ToString (); C_art. Fircatalogid = Fircatalogid; /* Parameter start/Mypage.itemcount = Convert.ToInt32 (C_art. Bind_countbyfircatalogid ()); mypage.currentpage = Int. Parse (request["page"). ToString ()); Mypage.pagenumber = 10; Mypage.eachpageitemcount = 20; Mypage.pageurl = "Showalllist.aspx?fircatalogid=" + Fircatalogid + "&page="; /* Parameter end */C_art. currentpage = Int. Parse (request["page"). ToString ()); C_art. PageSize = 20; C_art. Gview_showalllistbyfircatalogid = This.gview_showalllistbyfircatalogid; C_art. Bind_alllistbyfircatalogid ();

7. Style of pagination control

. Userpage/* Pagination style * * margin-top:5px; padding-left:0px; font-size:15px; color: #333333; text-align:right; Font-family:ari Al, Helvetica, Sans-serif; } . Userpage A/* pagination style * * display:block; text-decoration:none; margin-right:5px; padding-top:2px; padding-right:5px; paddi ng-bottom:2px; padding-left:5px; border:1px solid #88C7FB; Float:left; font-size:12px; Font-weight:normal; } . Userpage strong/* Pagination style * * * display:block; text-decoration:none; margin-right:5px; padding-top:2px; padding-right:5px; padding-bottom:2px; padding-left:5px; border:1px solid #88C7FB; Float:left; Background-color:silver; font-size:14px; Font-weight:bold; } . Userpage a:hover/* pagination style * * display:block; text-decoration:none; border:1px solid #CCCCCC;}

8. Concluding remarks;

This is a paging control method that I think is best used in. NET, and using stored procedure paging greatly improves the efficiency of data access.

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.