ASP.NET真假分頁—真分頁

來源:互聯網
上載者:User

標籤:des   style   blog   http   os   使用   io   strong   ar   



承接上篇博文《ASP.NET真假分頁-假分頁》:http://blog.csdn.net/u010773667/article/details/38845009,繼續講解ASP.NET真假分頁之真分頁。

 

真分頁



當資料量過大,有幾萬甚至十幾萬條資料時,每次都從資料庫中取出所有資料就會降低查詢效率,系統運行慢,還有可能卡死,這時假分頁就會顯得很不人性化,因此有了真分頁的必要性。


 正如上篇博文總結歸納,“真”相對於“假”存在,即不藕斷絲連,從根部徹底斷開,在此處表現為根據查詢條件,只從資料庫中提取出需要的部分,適合於大資料。而真分頁的實現要藉助於第三方控制項AspNetPager。



AspNetPager控制項是一個基於.net的第三方免費開原始檔控制,具有開發高效、使用方便、功能完整等優點。它彌補了GridView內建分頁以及PageDatasource類輔助分頁的不足,將分頁資料邏輯和頁面UI分離開來,非常有利於SQL分頁的實現。

 

首先需要下載AspNetPager控制項:http://www.webdiyer.com/downloads/




在VS中引用AspNetPager控制項,歡迎參考博文《VS添加Ajax》中添加選擇項部分(有圖有真相),此處不再贅述:http://blog.csdn.net/u010773667/article/details/38518461


 首先在web表單中拖放一個gridview控制項用來顯示資料,選中AspNetPager控制項拖拽到web表單相應位置用來進行分頁設定。顯示如下:


對分頁提示資訊進一步改進。選中spNetPager控制項,在右下角將會顯現一個小按鈕,單擊開啟,對導覽按鈕顯示文本進行設定


改進效果:


對頁索引文本或下拉框進行設定 


改進效果見:


如果還想顯示的更加具體,可進行自訂資訊區顯示方式及內容設定



上述對控制項進行的所有設定將在VS中自動產生相應代碼,so我們也可以通過手動輸入代碼進行設定。



好了,設定好了前台,接下來就要進行資料繫結了


protected void Page_Load(object sender, EventArgs e)        {                      if (!Page .IsPostBack )            {                string caid = Request.QueryString["caid"];                DataTable dt = new NewsManager().SelectAllNewsByCaId(caid);                             anpCa.AlwaysShow = true;                anpCa.PageSize = 5;                anpCa.RecordCount = dt.Rows.Count;                int startIndex = anpCa.PageSize * 0;                int endIndex = anpCa.PageSize * 1;                               gvDataBind(caid, startIndex, endIndex);                           }        }         private void gvDataBind(string caid,int startIndex,int endIndex)        {                       DataTable dt = new NewsManager().SelectPartNewsByCaId(caid, startIndex, endIndex);            if (dt.Rows.Count != 0)            {                lblCategory.Text = dt.Rows[0]["name"].ToString();  //使類別標題顯示相應的類別名稱            }            gvNew.DataSource = dt;            gvNew.DataBind();        }      protected void anpCa_PageChanged(object sender, EventArgs e)        {            string caid = "6";            int startIndex = anpCa.PageSize * (anpCa.CurrentPageIndex - 1)+1;            int endIndex = anpCa.PageSize * (anpCa.CurrentPageIndex);            gvDataBind(caid, startIndex, endIndex);        }}


在D層資料查詢的部分代碼展示

</pre></p><p></p><pre class="html" name="code">
</pre><span style="font-family:華文楷體;font-size: 14pt;"></span><pre class="html" name="code">#region 根據類別ID取出該類別下的所有新聞的分頁顯示        /// <summary>        /// 根據類別ID取出該類別下的所有新聞        /// </summary>        /// <param name="caId">類別ID</param>        /// <returns></returns>        public DataTable SelectPartNewsByCaId(string caId,int startIndex, int endIndex)        {            DataTable dt = new DataTable();            SqlParameter[] paras = new SqlParameter[]{           new SqlParameter ("@caId",caId ),           new SqlParameter ("@startIndex",startIndex ),           new SqlParameter ("@endIndex",endIndex )    };            dt = sqlhelper.ExecuteQuery("dbo.category_showpage", paras, CommandType.StoredProcedure);            return dt;        }        #endregion


預存程序(很重要)


-- =============================================-- Author:王英群-- Create date: 2014-8-10-- Description:跟據類別ID取出該類別下的所有新聞的分頁顯示-- =============================================ALTER PROCEDURE [dbo].[category_showpage] @caid int,@startIndex int,@endIndex intASBEGINwith temptable as (    select ROW_NUMBER() over (order by id desc) as 行號, * from        (            select n.id,n.titile,n.createTime,c.[name],n.caId from news n              inner join category c on n.caId =c.id and n.caId [email protected]        ) as aa) select * from temptable  where 行號 between @startIndex and @endIndex    END

運行後效果見:




注意:我的程式中多了一個參數(類別ID),在頁索引動態變化的過程中需要一直重新獲得,這一點我沒有實現,希望小夥伴們可以協助我,謝謝!



結合上篇博文,假分頁適合於資料量相對較小的情況下,而真分頁適合於資料量大的情況下。真假分頁的使用,為我們的閱讀減負。









ASP.NET真假分頁—真分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.