asp.net Repeater 百萬級分頁

來源:互聯網
上載者:User

開發平台是2003,所用控制項是Repeater

using System;
using System.Data;
using System.Data.SqlClient;

namespace Pagination.Class
{
/// <summary>
/// Pager 的摘要說明。
/// </summary>
public class Pager
{
   public Pager()
   {
    //
    // TODO: 在此處添加建構函式邏輯
    //
   }
   /// <param name="fieldlist"></param>欄位列表,如id,title,content
   /// <param name="condition"></param>條件,如id<100
   /// <param name="pkey"></param>主鍵,自增
   /// <param name="tablename"></param>表的名字,如News
   /// <param name="sort"></param>排序,0表示降序,1表示升序
   /// <param name="pagesize"></param>每頁大小
   /// <param name="cpage"></param>當前頁碼
   public DataSet GetCurrentDataSet(string fieldlist,string condition,string pkey,string tablename,int sort,int pagesize,int cpage)//得到當前頁記錄
   {
    SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cs"]);
    SqlDataAdapter cmd=new SqlDataAdapter(GetSql(fieldlist,condition,pkey,tablename,sort,pagesize,cpage),conn);
    DataSet ds=new DataSet();
    cmd.Fill(ds);
    return ds;
   }
   static string GetSql(string fieldlist,string condition,string pkey,string tablename,int sort,int pagesize,int cpage)
   {
    string sql="";
    if(sort==0)
    {
     if(condition!="")
     {
      sql="select top "+pagesize.ToString()+" "+fieldlist+" from "+tablename+" where "+condition+" and "+pkey+" not in(select top "+pagesize*(cpage-1)+" "+pkey+" from "+tablename+" where "+condition+" and order by "+pkey+" desc) order by "+pkey+" desc";
     }
     else
     {
      sql="select top "+pagesize.ToString()+" "+fieldlist+" from "+tablename+" where "+pkey+" not in(select top "+pagesize*(cpage-1)+" "+pkey+" from "+tablename+" order by "+pkey+" desc) order by "+pkey+" desc";
     }
    }
    else
    {
     if(condition!="")
     {
      sql="select top "+pagesize.ToString()+" "+fieldlist+" from "+tablename+" where "+condition+" and "+pkey+" not in(select top "+pagesize*(cpage-1)+" "+pkey+" from "+tablename+" where "+condition+" and order by "+pkey+" asc) order by "+pkey+" asc";
     }
     else
     {
      sql="select top "+pagesize.ToString()+" "+fieldlist+" from "+tablename+" where "+pkey+" not in(select top "+pagesize*(cpage-1)+" "+pkey+" from "+tablename+" order by "+pkey+" asc) order by "+pkey+" asc";
     }
    }
    return sql;
   }
   public int GetCounts(string pkey,string tablename,string condition)//得到總記錄數
   {
    string sql;
    if(condition!="")
    {
        sql="select count("+pkey+") from "+tablename;
    }
    else
    {
        sql="select count("+pkey+") from "+tablename;
    }
    SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["cs"]);
    SqlCommand cmd=new SqlCommand(sql,conn);
    conn.Open();
    SqlDataReader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
    dr.Read();
    string str=dr[0].ToString();
    dr.Close();
    return Convert.ToInt32(str);
   }
}
}
這裡可以輸出總記錄數和當前頁面的dataset。

針對此通用分頁類的調用執行個體
1 建表News
CREATE TABLE [News] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[title] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[content] [text] COLLATE Chinese_PRC_CI_AS NULL ,
CONSTRAINT [PK_News] PRIMARY KEY CLUSTERED
(
   [id]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
2 建立產生資料的預存程序
create proc addnews
@count int
as
declare @i int
set @i=0
set nocount on
while @i<@count
begin
set @i=@i+1
insert into news (title,content) values ('百萬級資料分頁測試','Test by 逐日★風')
end
set nocount off
GO
3 產生資料
addnews 2000000

4 建立Aspx檔案
<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
   <table width="800" border="1" cellpadding="3">
</HeaderTemplate>
<ItemTemplate>
   <tr>
    <td><%#DataBinder.Eval(Container.DataItem,"id")%></td>
    <td><%#DataBinder.Eval(Container.DataItem,"title")%></td>
    <td><%#DataBinder.Eval(Container.DataItem,"content")%></td>
   </tr>
</ItemTemplate>
<FooterTemplate>
   </table>
</FooterTemplate>
</asp:Repeater>
<div align="center"><FONT face="宋體">共</FONT>
<asp:Label id="lblcount" runat="server"></asp:Label><FONT face="宋體">條記錄,當前
   <asp:Label id="lblcurrent" runat="server"></asp:Label>頁&nbsp;&nbsp;
   <asp:LinkButton id="hlprev" runat="server">上頁</asp:LinkButton>&nbsp;
   <asp:LinkButton id="hlnext" runat="server">下頁</asp:LinkButton>&nbsp;&nbsp;
   <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList></FONT></div>
5 Cs檔案
protected System.Web.UI.WebControls.Label lblcount;
   protected System.Web.UI.WebControls.Label lblcurrent;
   protected System.Web.UI.WebControls.DropDownList DropDownList1;
   protected System.Web.UI.WebControls.LinkButton hlprev;
   protected System.Web.UI.WebControls.LinkButton hlnext;
   protected System.Web.UI.WebControls.Repeater Repeater1;
   private int pagesize=20;
   static int cpage;
   static int TotalPages;

   private void Page_Load(object sender, System.EventArgs e)
   {
    // 在此處放置使用者代碼以初始化頁面
    if(!this.IsPostBack)
    {
     cpage=1;
     Class.Pager pg=new Class.Pager();
     int Counts=pg.GetCounts("id","News","");
     lblcount.Text=Counts.ToString();//得到總記錄數

     TotalPages=Counts/pagesize;
     if(Counts<pagesize)
      TotalPages=1;
     else
     {
      if(Counts%pagesize!=0)TotalPages++;
     }//計算總頁數

     lblcurrent.Text=cpage.ToString()+"/"+TotalPages;
    
     DataSet ds=new DataSet();
     ds=pg.GetCurrentDataSet("id,title,content","","id","News",1,10,cpage);

     bind(ds);
     Gethl();
     Getddlist();
    }
   }
   private void Getddlist()
   {
    if(TotalPages!=1)
    {
     for(int i=1;i<=TotalPages;i++)
     {
      DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString()));
     }
    }
    else
    {
     DropDownList1.Items.Add(new ListItem("1","1"));
    }
   }
   private void Gethl()//得到上頁,下頁狀態
   {
    if(lblcurrent.Text=="1")
    {
     hlprev.Enabled=false;
     hlnext.Enabled=false;
    }
    else
    {
     if(cpage==1)
      hlprev.Enabled=false;
     else
      hlprev.Enabled=true;
     if(cpage==Convert.ToInt32(TotalPages))
      hlnext.Enabled=false;
     else
      hlnext.Enabled=true;
    }
    lblcurrent.Text=cpage.ToString()+"/"+TotalPages;
   }
   private void bind(DataSet ds)
   {
    Repeater1.DataSource=ds;
    Repeater1.DataBind();
   }
   #region Web Form設計器產生的程式碼
   override protected void OnInit(EventArgs e)
   {
    //
    // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
   }
  
   /// <summary>
   ///   設計器支援所需的方法 - 不要使用代碼編輯器
   ///   修改此方法的內容。
   /// </summary>
   private void InitializeComponent()
   {
    this.hlprev.Click += new System.EventHandler(this.hlprev_Click);
    this.hlnext.Click += new System.EventHandler(this.hlnext_Click);
    this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load);

   }
   #endregion

   private void hlprev_Click(object sender, System.EventArgs e)
   {
    cpage=cpage-1;
    Class.Pager pg=new Class.Pager();
    DataSet ds=new DataSet();
    ds=pg.GetCurrentDataSet("id,title,content","","id","News",cpage,10,cpage);
    bind(ds);
    Gethl();
    DropDownList1.ClearSelection();
    DropDownList1.Items.FindByValue(cpage.ToString()).Selected=true;
   }

   private void hlnext_Click(object sender, System.EventArgs e)
   {
    cpage=cpage+1;
    Class.Pager pg=new Class.Pager();
    DataSet ds=new DataSet();
    ds=pg.GetCurrentDataSet("id,title,content","","id","News",cpage,10,cpage);
    bind(ds);
    Gethl();
    DropDownList1.ClearSelection();
    DropDownList1.Items.FindByValue(cpage.ToString()).Selected=true;
   }
   private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
   {
    cpage=Convert.ToInt32(DropDownList1.SelectedValue);
    Class.Pager pg=new Class.Pager();
    DataSet ds=new DataSet();
    ds=pg.GetCurrentDataSet("id,title,content","","id","News",cpage,10,cpage);
    bind(ds);
    Gethl();
   }

本人機器Pentium M1.7G 480MDDRM ,可用實體記憶體70MB左右。200W資料平均分頁時間0.5秒左右。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.