介紹asp.net的幾種分頁方式
通常分頁有3種方法,分別是asp.net自帶的資料顯示空間如GridView等自帶的分頁,第三方分頁控制項如aspnetpager,預存程序分頁等。這裡分別做總結。
第一種:使用GridView自帶分頁,這種是最簡單的分頁方法。
前台的方法
<asp:GridView ID="GridView1" AllowPaging="true" runat="server" onpageindexchanging="GridView1_PageIndexChanging" PageSize="3"> </asp:GridView>
後台方法:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using JXSoft.TicketManage.Model; using JXSoft.TicketManage.BLL; using System.Text.RegularExpressions; using System.Data; namespace JXSoft.TicketManage.Web { public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { BindData(); } } protected void BindData() { DataTable dt=new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Name"); for (int i = 0; i < 10;i++ ) { dt.Rows.Add(i.ToString(), i.ToString()); } this.GridView1.DataSource = dt; this.GridView1.DataBind(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; BindData(); } } }
第二種:使用個人化顯示的AspNetPager.dll進行分頁
此處需要新增aspnetpager.dll的引用
前台:
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" > </asp:GridView> <webdiyer:AspNetPager ID="AspNetPager1" runat="server" CustomInfoHTML="第%CurrentPageIndex%页,共%PageCount%页,每页%PageSize%条" FirstPageText="首页" LastPageText="尾页" LayoutType="Table" NextPageText="下一页" onpagechanging="AspNetPager1_PageChanging" PageIndexBoxType="DropDownList" PagingButtonLayoutType="Span" PrevPageText="上一页" ShowCustomInfoSection="Left" ShowPageIndexBox="Always" SubmitButtonText="Go" PageSize="4" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到"> </webdiyer:AspNetPager> </div> </form>
後台:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using JXSoft.TicketManage.Model; using JXSoft.TicketManage.BLL; using System.Text.RegularExpressions; using System.Data; namespace JXSoft.TicketManage.Web { public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { BindData(); } } protected void BindData() { DataTable dt=new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Name"); for (int i = 0; i < 10;i++ ) { dt.Rows.Add(i.ToString(), i.ToString()); } DataSet ds = new DataSet(); ds.Tables.Add(dt); Pager(this.GridView1, this.AspNetPager1, ds); } protected void Pager(GridView dl, Wuqi.Webdiyer.AspNetPager anp, System.Data.DataSet dst) { PagedDataSource pds = new PagedDataSource(); pds.DataSource = dst.Tables[0].DefaultView; pds.AllowPaging = true; anp.RecordCount = dst.Tables[0].DefaultView.Count; pds.CurrentPageIndex = anp.CurrentPageIndex - 1; pds.PageSize = anp.PageSize; dl.DataSource = pds; dl.DataBind(); } protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) { AspNetPager1.CurrentPageIndex = e.NewPageIndex; BindData(); } } }
第三種:使用AspNetPager結合儲存程序進行分頁
這種方法分頁稍微複雜一些,但是可以應付比較大的資料量。
前台:
<asp:GridView ID="GridView1" runat="server" CssClass="GridTable" AutoGenerateColumns="false" onrowdatabound="GridView1_RowDataBound" > </asp:GridView> <webdiyer:AspNetPager ID="AspNetPager1" runat="server" CustomInfoHTML="第%CurrentPageIndex%页,共%PageCount%页,每页%PageSize%条" FirstPageText="首页" LastPageText="尾页" LayoutType="Table" NextPageText="下一页" onpagechanged="AspNetPager1_PageChanged" PageIndexBoxType="DropDownList" PagingButtonLayoutType="Span" PrevPageText="上一页" ShowCustomInfoSection="Left" ShowPageIndexBox="Always" SubmitButtonText="Go" PageSize="4" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到"> </webdiyer:AspNetPager>
後台:
//绑定方法中需要传递aspnetpager的两个属性 protected void DataBind(){ DataSet ds = reportQueryBLL.GetTcikDetailReport(this.txtStartDate.Text,this.txtEndDate.Text,int.Parse( this.DropDownListPartment1.SelectedValue), this.txtPayPerson1.Text,this.txtTicketNum.Text,this.txtTicketNo.Text, AspNetPager1.StartRecordIndex,AspNetPager1.EndRecordIndex);//注意最后两个参数是aspnetpager的属性。 this.GridView1.DataSource = ds; this.GridView1.DataBind(); } //分页控件的页索引变化事件 protected void AspNetPager1_PageChanged(object src, EventArgs e) { BindDetailReportToGv(); } //page_base中需要加载首次的数据条数 DataSet ds = reportQueryBLL.GetDetail(this.txtStartDate.Text, this.txtEndDate.Text, int.Parse(this.DropDownListPartment1.SelectedValue), this.txtPayPerson1.Text, this.txtTicketNum.Text, this.txtTicketNo.Text); this.AspNetPager1.RecordCount = ds.Tables[0].Rows.Count; BindDetailReportToGv();
這裡用的預存程序比較複雜,因為SQL語句沒有能夠放到檢視中,也無法直接從表格中找出結果,這個儲存過程有點變態,如果有朋友看到了,希望能指點一下。
其實預存程序的核心在於:
Create PROCEDURE [dbo].[P_GetPagedOrders2005] (@startIndex INT, @endindex INT ) AS select * from (SELECT ROW_NUMBER() OVER(ORDER BY IPid DESC) AS rownum, [IPid],[IPFrom],[IPTo],[IPLocation],[IPCity],[IPToNumber],[IPFromNumber] from IPInfo) as U WHERE rownum between @startIndex and @endIndex GO
以上是介紹asp.net的幾種分頁方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP開發:如何實現表格資料排序和分頁功能在進行Web開發中,處理大量資料是一項常見的任務。對於需要展示大量資料的表格,通常需要實現資料排序和分頁功能,以提供良好的使用者體驗和最佳化系統效能。本文將介紹如何使用PHP實作表格資料的排序和分頁功能,並給出具體的程式碼範例。排序功能實作在表格中實作排序功能,可以讓使用者根據不同的欄位進行升序或降序排序。以下是一個實作表格

CakePHP是一個強大的PHP框架,為開發人員提供了許多有用的工具和功能。其中之一是分頁,它可以幫助我們將大量資料分成幾頁,從而簡化瀏覽和操作。預設情況下,CakePHP提供了一些基本的分頁方法,但有時你可能需要建立一些自訂的分頁方法。這篇文章將向您展示如何在CakePHP中建立自訂分頁。步驟1:建立自訂分頁類別首先,我們需要建立一個自訂分頁類別。這個

隨著數據的不斷增長,表格顯示變得更加困難。大多數情況下,表格中的資料量過大,導致表格在載入時變得緩慢,而且使用者需要不斷地瀏覽頁面才能找到自己想要的資料。本文將介紹如何使用JavaScript實作表格資料的分頁顯示,讓使用者更容易找到自己想要的資料。一、動態建立表格為了讓分頁功能更可控,需要動態建立表格。在HTML頁面中,新增一個類似下面的table元素。

如何使用JavaScript實作表格分頁功能?隨著網路的發展,越來越多的網站都會使用表格來展示數據。在某些資料量較大的情況下,需要將資料進行分頁展示,以提升使用者體驗。本文將介紹如何使用JavaScript實作表格分頁功能,並提供具體的程式碼範例。一、HTML結構首先,我們需要準備一個HTML結構來承載表格和分頁按鈕。我們可以使用<tab

MyBatis是一個優秀的持久層框架,它支援基於XML和註解的方式操作資料庫,簡單易用,同時也提供了豐富的插件機制。其中,分頁插件是使用頻率較高的插件之一。本文將深入探討MyBatis分頁外掛的原理,並結合具體的程式碼範例進行說明。一、分頁外掛原理MyBatis本身並沒有提供原生的分頁功能,但可以藉助外掛程式來實現分頁查詢。分頁插件的原理主要是透過攔截MyBatis

如何利用Layui開發一個具有分頁功能的資料展示頁面Layui是一個輕量級的前端UI框架,提供了簡潔美觀的介面元件和豐富的互動體驗。在開發中,我們經常會遇到需要展示大量資料並進行分頁的情況。以下是利用Layui開發的具有分頁功能的資料展示頁面的範例。首先,我們需要引入Layui的相關文件和依賴。在html頁面的<head>標籤中加入以下代

Vue元件實戰:分頁元件開發介紹在網路應用程式中,分頁功能是不可或缺的一個元件。一個好的分頁元件應該展示簡潔明了,功能豐富,而且易於整合和使用。在本文中,我們將介紹如何使用Vue.js框架來開發一個高度可自訂化的分頁元件。我們將透過程式碼範例來詳細說明如何使用Vue元件開發。技術堆疊Vue.js2.xJavaScript(ES6)HTML5和CSS3開發環

Vue是一種流行的JavaScript框架,用於建立使用者介面。在Vue技術開發中,實現分頁功能是常見的需求。本文將介紹如何使用Vue來實現分頁功能,並提供具體程式碼範例。在開始之前,我們需要提前準備一些基本知識。首先,我們需要了解Vue的基本概念和文法。其次,我們需要知道如何使用Vue元件來建立我們的應用程式。在開始之前,我們需要在Vue專案中安裝一個分頁插件,
