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

復制代碼

 

 

復制代碼
代碼


--下方可以忽略

 

--我用到的是存儲過程:

set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
go 

create PROCEDURE [dbo].[pro_pager] 
(@startIndex INT, 
@endindex INT, 
@strwhere varchar(200) 

AS 

SELECT  tb_On_Tick_Info.On_Tick_ID_Int,tb_On_Tick_Info.On_Tick_SellDatetime_Dtm,tb_On_Tick_Info.On_Tick_TicketsNum_Str,  tb_Department_Info.Dept_Name_Str,  tb_User_Info.User_Name_Str,  
  tb_On_Tick_Info.On_Tick_SellNumber_Str,  tb_On_Tick_Info.On_Tick_ShouldPay_Dec,  tb_On_Tick_Info.On_Tick_Count_Int,  
  tb_On_Tick_Info.On_Tick_Discount_Dec, tb_On_Tick_Details.On_Tick_Details_StartNo_Int, CHARINDEX(N'a',  
  tb_On_Tick_Info.On_Tick_Note_Text) AS Expr3, tb_User_Info_1.User_Name_Str AS Expr1,  tb_Ticket_Type.TicketType_Name_Dec,  
COUNT( tb_On_Tick_Details.On_Tick_Details_ID_Int) AS Expr2 ,tb_Department_Info.Dept_ID_Int 
into #temp 
FROM tb_User_Info INNER JOIN 
  tb_On_Tick_Info ON  tb_User_Info.User_ID_Int =  tb_On_Tick_Info.On_Tick_SellPerson_Int INNER JOIN 
  tb_Department_Info ON  tb_User_Info.User_DepartID_Int =  tb_Department_Info.Dept_ID_Int INNER JOIN 
  tb_User_Info AS tb_User_Info_1 ON  tb_On_Tick_Info.On_Tick_PayPerson_Int = tb_User_Info_1.User_ID_Int INNER JOIN 
  tb_On_Tick_Details ON  tb_On_Tick_Info.On_Tick_SellNumber_Str =  tb_On_Tick_Details.On_Tick_SellNumber_Str INNER JOIN 
  tb_Ticket_Type ON  tb_On_Tick_Details.On_Tick_Details_TicketsType_Int =  tb_Ticket_Type.TicketType_ID_Int  
where 1=1 +@strwhere 
GROUP BY  tb_On_Tick_Info.On_Tick_SellDatetime_Dtm,tb_On_Tick_Info.On_Tick_TicketsNum_Str,  tb_Department_Info.Dept_Name_Str,  tb_User_Info.User_Name_Str,  
   tb_On_Tick_Info.On_Tick_SellNumber_Str,  tb_On_Tick_Info.On_Tick_ShouldPay_Dec,  tb_On_Tick_Info.On_Tick_Count_Int,  
   tb_On_Tick_Info.On_Tick_Discount_Dec, CHARINDEX(N'a',  tb_On_Tick_Info.On_Tick_Note_Text), tb_User_Info_1.User_Name_Str,  
   tb_Ticket_Type.TicketType_Name_Dec,  tb_On_Tick_Details.On_Tick_Details_StartNo_Int ,tb_Department_Info.Dept_ID_Int,tb_On_Tick_Info.On_Tick_ID_Int 

declare @sql varchar(8000) 
set @sql = 'select  CONVERT(varchar(12) , On_Tick_SellDatetime_Dtm, 111 ) as On_Tick_SellDatetime_Dtm,Dept_Name_Str,User_Name_Str,On_Tick_SellNumber_Str,convert(varchar(15), On_Tick_ShouldPay_Dec) as On_Tick_ShouldPay_Dec,On_Tick_Count_Int,On_Tick_Discount_Dec' 
select @sql=@sql+',sum(case tickettype_name_dec when '''+tickettype_name_dec+''' then expr2 else 0 end) ['+tickettype_name_dec+']' 
from (select distinct tickettype_name_dec from tb_Ticket_Type ) as a 
set @sql=@sql+' ,expr3,Expr1,On_Tick_TicketsNum_Str,Dept_ID_Int,On_Tick_ID_Int into ##t from #temp  
group by  On_Tick_SellDatetime_Dtm,Dept_Name_Str,On_Tick_TicketsNum_Str,User_Name_Str,On_Tick_SellNumber_Str,On_Tick_ShouldPay_Dec,On_Tick_Count_Int, 
On_Tick_Discount_Dec ,expr3,Expr1,Dept_ID_Int,On_Tick_ID_Int order by On_Tick_SellDatetime_Dtm ' 
exec( @sql ) 

--select * from ##t 

select * from (SELECT ROW_NUMBER() OVER(ORDER BY on_tick_id_int DESC) AS rownum, 
* from ##t) as U 
WHERE rownum between @startIndex and @endIndex 

drop table ##t 
復制代碼
 
 
轉自:http://www.cnblogs.com/wengyuli/archive/2010/06/08/aspnet-pager-three-method.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM