自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析ASP.NET高效分頁的實現(xiàn)過程

開發(fā) 后端
本文將介紹的是ASP.NET高效分頁的實現(xiàn)過程,實例是以上一頁和下一頁的方式實現(xiàn)的。

本文要實現(xiàn)的是以上一頁和下一頁的方式實現(xiàn)的ASP.NET高效分頁,至于以1、2、3、4這樣的形式顯示的ASP.NET高效分頁,還有待于作者的進一步研究ASP.NET高效分頁后實現(xiàn)。

簡單、高效這是我們追求的分頁效果。

現(xiàn)在有三種很常見的分頁:

1、分頁用的html和后臺代碼都自己寫 ,設(shè)計和代碼一般都只對應(yīng)某個網(wǎng)頁,難以在其他頁面很好的重用

2、最簡單的當(dāng)然是數(shù)據(jù)控件自帶的分頁功能,他的那些缺陷已經(jīng)被討論很多年了,我就不重復(fù)了,相信稍微有點魄力和職業(yè)態(tài)度的程序員都不會用那個分頁

3、自制的分頁控件,可以實現(xiàn)代碼和設(shè)計的分離,可以在多個頁面重用控件,但是缺陷是:每個頁面都得調(diào)用控件而且還要在頁面的后臺代碼里初始化控件,例如向控件里傳送總頁數(shù)、當(dāng)前分頁序號、頁面大小等

綜合以上分析,我打算自己做個簡單的分頁控件,思路如下:

1、首先必須實現(xiàn)分頁時代碼和設(shè)計的分離,例如“下一頁”,“上一頁”,他們的樣式寫在一個文件里,而把控制他們怎么顯示寫在另一個文件里,例如,到了最后一頁,“最后一頁”這個按鈕不能用。所以我寫了個template.html文件,這個描述了分頁時的樣式

  1. Code  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5.     <title></title> 
  6. </head> 
  7. <body> 
  8.     <div style="width: 100%; height: 30px; overflow: hidden; clear: both; font-size: 12px;" id="MyPagingString{10}"> 
  9.         <div style="float: left; width: 50px; line-height: 20px; text-align: center; height: 20px;  
  10.             border: 1px solid #d8dfea; margin-left: 15px; cursor: pointer; display: {0}"  
  11.             onmouseover="p.on({1},this)" onmouseout="p.out({1},this)" onclick="p.direct({1},'first','{10}')"> 
  12.             第一頁  
  13.         </div> 
  14.         <div style="float: left; width: 50px; line-height: 20px; text-align: center; height: 20px;  
  15.             border: 1px solid #d8dfea; margin-left: 15px; cursor: pointer; display: {0}"  
  16.             onmouseover="p.on({2},this)" onmouseout="p.out({2},this)" onclick="p.direct({2},'previous','{10}')"> 
  17.             上一頁  
  18.         </div> 
  19.         <div style="float: left; width: 50px; line-height: 20px; text-align: center; height: 20px;  
  20.             border: 1px solid #d8dfea; margin-left: 15px; cursor: pointer; display: {0}"  
  21.             onmouseover="p.on({3},this)" onmouseout="p.out({3},this)" onclick="p.direct({3},'next','{10}')"> 
  22.             下一頁  
  23.         </div> 
  24.         <div style="float: left; width: 65px; line-height: 20px; text-align: center; height: 20px;  
  25.             border: 1px solid #d8dfea; margin-left: 15px; cursor: pointer; display: {0}"  
  26.             onmouseover="p.on({4},this)" onmouseout="p.out({4},this)" onclick="p.direct({4},'end','{10}',{11})"> 
  27.             最后一頁  
  28.         </div> 
  29.         <div style="float: left; height: 20px; line-height: 20px; margin-left: 20px; display: {9}"> 
  30.             每頁記錄:<span style=" color:Red;">{5}</span>  當(dāng)前頁: <span style=" color:red;">{6} </span> 總頁數(shù): <span style=" color:Red;">{7}</span>  總記錄數(shù): <span style=" color:Red;">{8}</span> 
  31.         </div> 
  32.     </div> 
  33.  
  34.     <script type="text/javascript"> 
  35.         var divTab='MyPagingString{10}';  
  36.         var div=[{1},{2},{3},{4}];  
  37.         var p = {  
  38.             init: function() {  
  39.                     var pstr=document.getElementById(divTab).getElementsByTagName("div");  
  40.                     if (!div[0]){  
  41.                        pstr[0].style.color = '#ccc';}  
  42.                     if (!div[1]){  
  43.                        pstr[1].style.color = '#ccc';}  
  44.                     if (!div[2]){  
  45.                        pstr[2].style.color = '#ccc';}  
  46.                     if (!div[3]){  
  47.                        pstr[3].style.color = '#ccc';}  
  48.             },  
  49.             on: function(v, this_) {  
  50.                 if (v) {  
  51.                     this_.style.backgroundColor = '#3b5998'this_.style.color = '#fff';  
  52.             }},  
  53.             out: function(v, this_) {  
  54.                 if (v) {  
  55.                     this_.style.backgroundColor = '#fff'this_.style.color = '#000';  
  56.             }},  
  57.             direct:function(v,t,i){  
  58.             if (!v) {return;}  
  59.             var index=parseInt(i.split('|')[1]);  
  60.             var temp=i.split('|')[0]+'_paging_index=';  
  61.             var _cookie=document.cookie;  
  62.             var cookiekey=_cookie.substring(_cookie.indexOf(temp)+temp.length,_cookie.indexOf(";",_cookie.indexOf(temp)));  
  63.             document.cookie="paging_table="+i.split('|')[0];  
  64.             switch(t){  
  65.             case "first":  
  66.             document.cookie=temp+"0";  
  67.             break;  
  68.             case "previous":   
  69.             document.cookie=temp+(--index);  
  70.             break;  
  71.             case "next":   
  72.             document.cookie=temp+(++index);  
  73.             break;  
  74.             case "end":  
  75.             document.cookie=temp+arguments[3];  
  76.             break;  
  77.             }  
  78.             document.cookie="paging=1";  
  79.             document.forms[0].submit();  
  80.             }};  
  81.         p.init();  
  82.     </script> 
  83. </body> 
  84. </html> 

當(dāng)程序第一次加載時,從硬盤讀取分頁模板文件template.html并且放入緩存,如果第二次有分頁請求時就從緩存讀取,
如果,template.html,被修改則再次從硬盤讀取,類似asp.net里的配置文件讀取機制,

緩存代碼如下:

  1. Code  
  2. public static string GetPageHtml()  
  3.     {  
  4.         pagingHtml = (string)(HttpContext.Current.Cache["paging"]);  
  5.         if (string.IsNullOrEmpty(pagingHtml))  
  6.         {  
  7.             string path = null;  
  8.             CacheDependency cd;  
  9.             path = GetPagingTemplePath();  
  10.             cd = new CacheDependency(path);  
  11.             ReadPagingHtmlsFromDisk(path);     
  12.             HttpContext.Current.Cache.Insert("paging", pagingHtml, cd);  
  13.         }  
  14.         return pagingHtml;  
  15.     } 

2、對數(shù)據(jù)源的獲取的sql實現(xiàn)了優(yōu)化,下面是兩種常用的分頁語句,第二條語句的優(yōu)勢在于:

not in 會引起全表掃描,而且不會使用聚集索引,而第二條語句沒有這樣的缺陷

  1. select top size  * from table where id not in (select top  index*size id from table )  
  2. lect top size  * from table where id > (select max (id) from (select top  index*size id from tableas T ) 

對用戶輸入的sql語句,例如“select * from table”自動優(yōu)化成上面的第二種格式

下面這個方法實現(xiàn)了復(fù)雜sql語句轉(zhuǎn)化

  1. Code  
  2. public static string AnalyticsSql(string sql, int index, int size)  
  3.     {  
  4.         string keyid = null, columns = null, table = null, orderby = null, wherestr = null, originalSql = null;  
  5.         originalSql = sql;  
  6.         originalSql = originalSql.Replace(originalSql.Substring(originalSql.IndexOf(" select ") + 8, originalSql.IndexOf(" from ") - 8 - originalSql.IndexOf(" select ")), " count(*) ");  
  7.         if (sql.IndexOf(" * ") != -1)  
  8.         {  
  9.             if (sql.IndexOf("|") != -1)  
  10.             {  
  11.                 keyid = sql.Substring(sql.IndexOf("|") + 1, sql.IndexOf(" ", sql.IndexOf("|")) - sql.IndexOf("|") - 1);  
  12.             }  
  13.             else 
  14.             {  
  15.                 keyid = "id";  
  16.             }  
  17.             columns = "*";  
  18.         }  
  19.         else 
  20.         {  
  21.             keyid = sql.Substring(sql.IndexOf("select") + 6, sql.IndexOf(",") - sql.IndexOf("select") - 6);  
  22.             columns = sql.Substring(sql.IndexOf("select") + 6, sql.IndexOf(" from ") - 6 - sql.IndexOf("select"));  
  23.         }  
  24.         if (sql.IndexOf(" where ") != -1)  
  25.         {  
  26.             wherestr = " where ";  
  27.             if (sql.IndexOf(" order ") != -1)  
  28.                 wherestr += sql.Substring(sql.IndexOf(" where ") + 7, sql.IndexOf(" order ") - sql.IndexOf(" where ") - 7);  
  29.             else 
  30.                 wherestr += sql.Substring(sql.IndexOf(" where ") + 7);  
  31.         }  
  32.         table = GetSqlTable(sql);  
  33.         if (sql.IndexOf(" order ") != -1)  
  34.         {  
  35.             orderby = sql.Substring(sql.LastIndexOf("by") + 2);  
  36.         }  
  37.         else 
  38.         {  
  39.             orderby = keyid;  
  40.         }  
  41.  
  42.         sql = "select top " + size.ToString() + " " + columns + " from " + table + " where  " + keyid + ">isnull((select max (" + keyid + ") from (select top " + (index * size).ToString() + " " + keyid.ToString() + " from " + table + wherestr + " order by " + orderby + ") as T),0) order by " + keyid;  
  43.         return originalSql + ";" + sql;  
  44.     } 

需要補充的是分頁排序時id問題:

如果你的SQL語句寫成了這樣:

  1. 1、select * from table where ... order ...  
  2. 則優(yōu)化后的sql以id排序  
  3.  
  4. 2、select *|CustomerId from table where ... order ...  
  5. 則優(yōu)化后的sql以CustomerId排序  
  6.  
  7. 2、select CustomerId,CustomerName,... from table where ... order ...  
  8. 則優(yōu)化后的sql以CustomerId排序   
  9. ==================================  


然后根據(jù)當(dāng)前信息格式化分頁顯示的htmls,例如,頁數(shù)、頁號、總記錄數(shù)、以及上下頁按鈕是否可用。具體代碼:

  1. Code  
  2.    public static string AnalyticsPagingHtmls(string tableAndindex,int count, int size, int index)  
  3.     {  
  4.         string _GetPageHtml = GetPageHtml();  
  5.         return string.Format  
  6.             (  
  7.             _GetPageHtml.Substring(0, _GetPageHtml.IndexOf(",{4}];") + 6),  
  8.             count == 0 || count <= size ? "none" : "",  
  9.             index == 0 ? "0" : "1",  
  10.             index == 0 ? "0" : "1",  
  11.             (index + 1 == ((count % size) == 0 ? count / size : ((count / size) + 1))) ? "0" : "1",  
  12.             (index + 1 == ((count % size) == 0 ? count / size : ((count / size) + 1))) ? "0" : "1",  
  13.             size,  
  14.             index + 1,  
  15.             (count % size) == 0 ? count / size : (count / size) + 1,  
  16.             count,  
  17.             count == 0 ? "none" : "",  
  18.             tableAndindex,  
  19.             ((count % size) == 0 ? count / size : ((count / size) + 1))-1  
  20.             )  
  21.             + _GetPageHtml.Substring(_GetPageHtml.IndexOf(",{4}];") + 6);  
  22.     } 

 如何使用這個分頁方法:

第一步:在配置文件里寫下如下代碼:

  1. Code  
  2.   <configSections> 
  3.     <section name="MyPaging" type="System.Configuration.NameValueSectionHandler"/> 
  4.   </configSections> 
  5.   <MyPaging> 
  6.   <add key="Paging" value="~/Paging/template.htm"/> 
  7.   </MyPaging> 

 第二步:在cs文件里,直接調(diào)用就行

  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if(MyPaging.IsPaging)  
  4.         {  
  5.             p1.InnerHtml = MyPaging.ExecutePaging(MyRep, "select CustomerId,ShipName,ShipAddress,ShippedDate from orders ", 0,5);  
  6.             p2.InnerHtml = MyPaging.ExecutePaging(MyRep2, "select CustomerID,CompanyName,ContactName,Address from dbo.Customers", 0,5);  
  7.         }  
  8.     } 


前臺代碼:

  1. Code  
  2. <%@ Page Language="C#"   AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  4. <html xmlns="http://www.w3.org/1999/xhtml"> 
  5. <head runat="server"> 
  6.     <title></title> 
  7. </head> 
  8. <body> 
  9.     <form id="form1" runat="server"> 
  10.     <asp:Repeater ID="MyRep" runat=server> 
  11.     <ItemTemplate> 
  12.     <div style="width:100%; height:20px;"> 
  13.     <%# Eval("CustomerID") %> 
  14.     <%# Eval("ShipName") %> 
  15.     <%# Eval("ShipAddress") %> 
  16.     <%# Eval("ShippedDate")%> 
  17.     </div> 
  18.     </ItemTemplate> 
  19.     </asp:Repeater> 
  20.     <div id="p1" runat=server></div> 
  21.       
  22.     <asp:Repeater  ID="MyRep2" runat=server> 
  23.     <ItemTemplate> 
  24.     <div style="width:100%; height:20px;"> 
  25.     <%# Eval("CustomerID")%> 
  26.     <%# Eval("CompanyName")%> 
  27.     <%# Eval("ContactName")%> 
  28.     <%# Eval("Address")%> 
  29.     </div> 
  30.     </ItemTemplate> 
  31.     </asp:Repeater> 
  32.     <div id="p2" runat=server></div> 
  33.     </form> 
  34. </body> 
  35. </html> 

實現(xiàn)效果:

效果圖 

原文標題:asp.net簡單&高效的分頁實現(xiàn),請大家提提意見

鏈接:http://www.cnblogs.com/chenxumi/archive/2009/11/05/1596777.html

 

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2012-04-23 15:10:18

ASP.NET

2009-07-28 14:47:18

ASP.NET MVC

2009-08-04 10:02:36

中國站長站

2009-07-28 10:01:16

ASP.NET Exc

2009-07-27 15:34:11

MembershipASP.NET

2009-07-27 10:18:12

TypeResolveASP.NET

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET優(yōu)點

2009-09-10 09:50:47

ASP.NET MVC

2009-07-23 10:37:43

2009-07-22 18:03:00

ASP.NET ASP

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-08-05 16:59:55

ASP.NET組件設(shè)計

2009-07-24 10:53:51

ASP.NET實現(xiàn)靜態(tài)

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計

2009-07-29 14:12:45

ASP.NET tra

2009-07-28 10:59:13

ASP.NET IIS

2009-08-12 18:19:46

ASP.NET報表打印
點贊
收藏

51CTO技術(shù)棧公眾號