Publish a paged algorithm, I hope everyone will discuss together, completely solve the bitterness of paging

xiaoxiao2021-03-06  51

Using system; using system.collections; using system.web.ui; using system.data; using system.data.sqlclient; using workstatic.database;

namespace ShareSoft.PageClass {///

/// paging operation class /// public class oppage: IDisposable {#region Constructors public oppage (StateBag _ViewState) {ViewState = _ViewState;} #endregion

Private statebag viewState = new statebag (); #Region Get or set up a database connection string private string _connection = string.empty; ///

/// Get or set up database connection string /// public String link {get {return _connection;} set {_connection = value;}} #ENDREGON

#REGON Get or set the table name ///

/// Get or set the table name /// public string Tablename {get {return convert.tostring (ViewState [TABLENAME "]);} set { ViewState ["Tablename"] = value;}} #endregion #region Get or set the current page number private int _CURPAGE = 1; /// /// get or set the current page number /// public INT Curpage {Get {Return_Curpage;} set {_curpage = value;}} #ENDREGON

#Region Get or sets the number of ///

/// ////////////////mmary> public int pageize {get {ix ("pagesize"] == NULL) ViewState ["iScount"] = 10; return configuration ["pagesize"]);} set {viewState ["Pagesize"] = value;}} #endregion

#Region whether the total record number ////

/// is calculated to calculate the total record number /// public enum counttype {/// /// Computing total record number /// yes = 1, /// /// Does not calculate the total record number /// NO = 0} /// /// Get or set whether the total record number is calculated //// / public counttype iscount {get {i (viewstate ["sCount"] == null) ViewState ["iScount"] = counttype.no; return (counttype) ViewState [iScount "];} set {viewstate "IScount"] = value;}} # endregion # region Get or sets Sorting Expression /// /// Gets or Sets Sorting Expression /// Public String OrdereXpression {Get {Return Convert. TOSTRING (ViewState "]);} set {viewState [" OrdereExpression "] = value.toupper ();}} #endregion #region Get or set the list of fields to display /// /// get Or set the list of fields to display /// public string fieldList {get {return control.tostring (ViewState ["FieldList"]);} set {viewState ["FieldList"] = value.toupper ();} } #Endregion

#Region Get or set the primary key field ///

/// to display or set the primary key field to display //// public string keyfield {get {return convert.toTRING (ViewState ["keyfield" ]);} Set {viewState ["keyfield"] = value;}} #endregion

#Region Get or set Query Condition ///

/// Get or Set Query Condition //// Public String CONDIPRENPRESSION {Get {Return Convert.TOSTRING (ViewState ["ConditioneExpression"]);} set { ViewState ["ConditioneExpression"] = value;}} #endregion #region Get or sets the total number of pages /// /// Number ///// / summary> public int pagecount {get {Return Convert.TOINT32 (ViewState ["PageCount"]);} set {viewstate ["PageCount"] = value;}} # endregion # region total record number /// /// total record number /// public int recordcount {get {return convert.Toint32 (ViewState ["RecordCount"]);} set {viewstate ["recordcount"] = value;}} #ENDREGION

#Region desired page DataView private DataView _PageDataView = null; desired ///

/// page DataView /// public DataView PageDataView {get {return _PageDataView;} set {_PageDataView = value; } #ENDREGION

#Region Set the total page number ///

/// Set the total number ////////////////////////////////////////////- ((recordcount% pagesize)> 0) {pageCount = (Recordcount / Pagesize) 1; else {pagecount = recordcount / pagesize;}} #ENDREGION

#REGON Piente Social Method ///

/// Biographical Master Method /// Public Void Main () {if (OrdeeExpression == "&& Keyfield! =" ") {OrdereExpression = Keyfield "ASC";

#Region Determine if you want to count the total record number count; if (iScount == counttype.no) {if (curpage == 1) {mustcount = counttype.yes;} else {mustcount = countType.no;}} else {Mustcount = Counttype.yes;} # endregion # region parameter sqlparameter [] ps = new sqlparameter [9]; ps [0] = new SQLParameter ("@ TableName", SqldbType.varchar, 50); PS [0] .value = TableName ;

PS [1] = New SqlParameter ("@ FieldList", SqldbType.varchar, 255); PS [1] .value = fieldList;

PS [2] = New Sqlparameter ("@ Keyfield", SqldbType.varchar, 50, keyfield); PS [2] .value = keyfield;

PS [3] = New SQLParameter ("@ Pagesize", SqldbType.Int); PS [3] .value = Pagesize;

PS [4] = New SQLParameter ("@ pageindex", sqldbtype.int); PS [4] .value = CURPAGE

PS [5] = New SQLParameter ("@ iScount", SqldbType.bit); PS [5] .value = (int).

PS [6] = New SQLParameter ("@ OrdeeExpression", SqldbType.varchar, 100); PS [6] .value = OrdereExpression;

PS [7] = New SqlParameter ("@ conditioneexpression", sqldbtype.varchar, 500); ps [7] .value = conditioneexpression;

PS [8] = New SqlParameter ("@ recordcount", sqldbtype.int); PS [8] .value = RecordCount;

#endregion DataSet ds = SqlHelper.ExecuteDataset (Link, CommandType.StoredProcedure, "up_Page_Result", ps); if (! ds == null || ds.Tables.Count = 2) {PageDataView = null; return;} else {RecordCount = Convert.Toint32 (DS.Tables [0] .Rows [0] [0]); if (MustCount == countType.yes) {setPageCount () {setPageCount ();} if (curpage> 1 && keyfield! = ") {Ix (Fieldlist) .Indexof ("*") == - 1) {string [] arrsortfield = OrdeeExpression.Replace ("DESC", ""). Replace ("ASC", "). Split (','); foreach (String SortField in ArrSortField) {if (FieldList.IndexOf (SortField.Trim ()) == - 1) {PageDataView = null; return;}}} ds.Tables [1] .DefaultView.Sort = OrderEexpression;} PageDataView = ds. Tables [1] .defaultview;} # endregion # region release object ///

/// Release Object /// public void dispose () {gc.suppressFinalize (this);} #ENDREGON} }

Stored Procedure - Gets Data Create Procedure DBo.up_page_result @tablename VARCHAR (50), - Table Name @fieldlist Varchar (255), - To display the name @keyfield varchar (50) = ', - - Home key field name @Pagesize int = 10, - Page size @PageIndex int = 1, - Total number of records @iscount bit = 0, - Return to record total, non-0 value returns @ORDEREEXPRESSION VARCHAR (100), - - Sort field and method (Note: Do not add order by) @conditioneexpression varchar (500) = ', - Query Conditions (Note: Don't add where) @recordcount int = 0 - total record number AS

Declare @execsql nvarchar (2000) - primary sentence

Declare @ReverseReradeExpression varchar (100) - Sort Type

SET @ OrderEexpression = UPPER (@OrderEexpression) SET @ OrderEexpression = 'ORDER BY' @ OrderEexpressionSET @ ReverseOrderEexpression = REPLACE (@ OrderEexpression, 'DESC', '###') SET @ ReverseOrderEexpression = REPLACE (@ ReverseOrderEexpression, 'ASC' , 'DESC') SET @ ReverseOrderEexpression = REPLACE (@ReverseOrderEexpression, '###', 'ASC') IF @ConditionEexpression! = '' SET @ConditionEexpression = 'WHERE' @ConditionEexpressionELSE SET @ConditionEexpression = 'WHERE 1 = 1 '

--- Total record number if @iscount! = 0 or @ recordcount = 0 Begin if (@ Keyfield = '') set @Execsql = 'select @ recordcount = count (' '1' ') from [' @TableName ']' @ConditionEexpression ELSE SET @EXECSQL = 'SELECT @ RecordCount = COUNT ([' @ KeyField ']) FROM [' @TableName ']' @ConditionEexpression --PRINT @EXECSQL EXECUTE sp_executesql @ eXECSQL, N '@RecordCount int OUTPUT', @RecordCount OUTPUT SET @EXECSQL = 'SELECT' STR (@RecordCount) 'AS RecordCount' EXEC (@EXECSQL) ENDELSE BEGIN SET @EXECSQL = 'SELECT' STR (@RecordCount ) 'As recordcount' exec (@execsql) End

IF @PageIndex = 1 Begin Set @Execsql = 'SELECT TOP' STR (@PageSize) '' @ FieldList 'from' Char (13) '[' @tablename '] @ConditioneExpression ' ' @OrderEexpression ENDELSE BEGIN IF (@ KeyField =' ') BEGIN DECLARE @TopNum int IF (@ PageIndex = CEILING (@RecordCount * 1.0 / @PageSize) AND @RecordCount% @PageSize <> 0) SET @ TopNum = @RecordCount % @PageSize Else Set @ TopNum = @PageSize Set @Execsql = 'SELECT TOP' STR (@topnum) '' @ FieldList 'from' Char (13) '[' @tablename '] a Where [' @ Keyfield '] in (SELECT TOP ' STR (@ PageIndex * @ Pagesize) Char (13) ' [' @keyfield '] from [' @tablename '] ' @ConditioneExpression '' CHAR (13) @OrderEexpression ')' @ ReverseOrderEexpression CHAR (13) END ELSE BEGIN DECLARE @BeginIndex int, @EndIndex int DECLARE @NewField varchar (100) SET @NewField = '[NEW_AutoId]' SET @BEG ININDEX = @ Pagesize * (@PageIndex-1) set @ endindex = @ Pagesize * @ PageIndex-1

SET @ EXECSQL = 'SELECT' @ NewField '= IDENTITY (int, 0,1),' @ FieldList 'INTO #tb FROM' @ TableName '' '' @ ConditionEexpression '' @OrderEexpression 'SELECT ' @ FieldList ' from #tb where ' @ newfield ' between ' str (@beginIndex) ' and ' str (@endindex) ' Drop table #tb 'end endprint @Execsqlexec (@execsql) Go

aspx page <% @ Page language = "c #" Codebehind = "pagetest.aspx.cs" AutoEventWireup = "false" Inherits = "WorkStatic.pagetest"%> <% @ Register TagPrefix = "MyCtl" Namespace = "ShareSoft.PageClass" Assembly = "Workstatic"%> PageTest </ title> <meta content = "Microsoft Visual Studio .NET 7.1 "Name =" generator "> <meta content =" c # "name =" code_language "> <meta content =" javascript "name =" vs_defaultclientscript "> <meta content =" http://schemas.microsoft.com / IntelliSense / IE5 "Name =" vs_targetschema "> </ head> <body> <form id =" form1 "method =" post "runat =" server "> <font face =" Song body "> <asp: datagrid id = "dgpage" runat = "server"> </ asp: datagrid> </ FONT> <MYCTL: ASPNETPAGER id = "anp1" runat = "server" UrlPaging = "False" ShowInputBox = "Always" CustomInfoTextAlign = "Right" HorizontalAlign = "Right" AlwaysSshow = "lastpagetext =" "prevpagetext =" Previous "nextpagetext =" Previous "firstpagetext =" Home "ShowCustomFosection =" LEFT "CustomFotext =" undefine "CUS TominFosectionWidth = "300px"> </ myctl: ASPNETPAGER> </ form> </ body> </ html> CS file:</p> <p>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 ShareSoft.pageClass; Namespace WorkStatic {/// <summary> /// pagetest's summary description. /// </ summary> public class pagetest: System.Web.UI.Page {protected System.Web.UI.WebControls.DataGrid dgpage; protected ShareSoft.PageClass.AspNetPager anp1; #region page initialization private void Page_Load (object sender, System.eventargs e) {if (! Page.ispostback) {mydatebind (1);}} #ENDREGON</p> <p>#Region Binding Data /// <Summary> /// Binding Data /// </ Summary> /// <param name = "page"> </ param> private vid mydatebind (int curpage) {USING mypage = new oppage (ViewState)) {mypage.Link = "data source = 127.0.0.1; initial catalog = xxx; password = xxxjj; user id = xxxyy"; mypage.TableName = "T_SoftInfo"; mypage.ConditionEexpression = "CONTAINS (SoftName, '/ "System /" OR / "Figure /"') "; mypage.pagesize = 10; mypage.curpage = curpage; mypage.keyfield =" softid "; mypage.ordereExpression =" Updatetime Desc, Softid Desc " Mypage.fieldlist = "*"; mypage.main (); this.dgpage.dataSource = mypage.pagedataview; this.dgpage.database = 10; this.anp1.pageSize = 10; this.nderdcount = mypage.recordcount THIS.Anp1.currentPageIndex = CURPAGE; this.anp1.database; this.anp1.custominfotext = "Current" CURPAGE " MyPage.PageCount " Total " MyPage.RecordCount " Summit " }} # EndRegion # Region Web Form Designer Generated Code Override Protected Void OnInit (Eventargs E) {// // Codegen: This call is an ASP.NET Web Form Designer It is necessary. // InitializeComponent (); base.onit (e);} /// <summary> /// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// </ summary> private void InitializeComponent () {this.anp1.PageChanged = new ShareSoft.PageClass.PageChangedEventHandler (this.anp1_PageChanged); this.Load = new System.EventHandler (this.Page_Load);</p> <p>} #Endregion</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-81291.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="81291" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '1EqryKrQG5dvjm3CMekfi2nxN1mfLsxHYjG3lgv7ZYtm338LiVIi9oGSVPoAXVQd1O_2FcjSe2L2LI9J8HI_2FRAPg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>