replica watches | rolex replica | replica rolex | replica watches | christian louboutin shoes | replica watches | fake watches | replica watches

浅析ASP.NET高效分页的实现过程(2)

来源: 未知   日期:2009-11-12  我要评论  胶粘剂 结构胶 硅胶 密封胶 中国胶粘剂网
需要补充的是分页排序时id问题: 如果你的SQL语句写成了这样: 1、 select * from table where ... order ... 则优化后的sql以id排序 2、 select *|CustomerId from table where ... order ... 则优化后的sql以Cust

需要补充的是分页排序时id问题:

如果你的SQL语句写成了这样:

  1. 1、select * from table where ... order ...  
  2. 则优化后的sql以id排序  
  3.  
  4. 2、select *|CustomerId from table where ... order ...  
  5. 则优化后的sql以CustomerId排序  
  6.  
  7. 2、select CustomerId,CustomerName,... from table where ... order ...  
  8. 则优化后的sql以CustomerId排序   
  9. ==================================  


然后根据当前信息格式化分页显示的htmls,例如,页数、页号、总记录数、以及上下页按钮是否可用。具体代码:

  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文件里,直接调用就行

  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> 

实现效果:

效果图 

顶一下
(0)
0%
踩一下
(1)
100%
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码: