用AspNetPager控件最详细的分页实现方法(亲自实践)

祝贺一下~好不容易把分页做出来了。之前都是用GridView自带的分页,版式难看不说,还极不优化,一次取出所有记录,然后进行假分页。
现在用AspNetPager控件做出的真分页,就好多了,不过还有改进的地方,SQL语句如果换成存储过程效率会更高。

首先在SqlHelper.cs(DAL层中的数据库助手类,用于写可以复用的基本增删查改方法)中加上以下代码:
ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
        
/// 获取分页数据
        
/// </summary>
        
/// <param name="sql">sql语句</param>
        
/// <param name="currentPage">当前页</param>
        
/// <param name="pagesize">每页显示数</param>
        
/// <param name="recordcount"></param>
        
/// <returns></returns>

        public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
ExpandedBlockStart.gifContractedBlock.gif        
{
            openCon();
            sqlDs.Clear();

            sqlDa 
= new SqlDataAdapter(sql, sqlConn);
            
int startRow = (currentPage - 1* pagesize;
            sqlDa.Fill(sqlDs, startRow, pagesize, 
"table");
            recordcount 
= GetPageRecord(sql);
            closeCon();

            
return sqlDs;
        }


        
//返回总的记录数
        public static int GetPageRecord(string sql)
ExpandedBlockStart.gifContractedBlock.gif        
{
            openCon();
            sql 
= Regex.Replace(sql, "order by.*""");
            sql 
= "select count(*) from (" + sql + ") as temp";
            sqlCmd 
= new SqlCommand(sql, sqlConn);
            
int recordcount = int.Parse(sqlCmd.ExecuteScalar().ToString());
            closeCon();

            
return recordcount;
        }

然后在BLL层新建一个PageManager.cs的分页操作类,封装一下DAL层方法:
ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
        
/// 获取分页数据
        
/// </summary>
        
/// <param name="sql">sql语句</param>
        
/// <param name="currentPage">当前页</param>
        
/// <param name="pagesize">每页显示数</param>
        
/// <param name="recordcount"></param>
        
/// <returns></returns>

        public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
return SQLHelper.GetPage(sql, currentPage, pagesize, out recordcount);
        }

需分页的数据绑定处是这样的:
ContractedBlock.gif ExpandedBlockStart.gif Code
<asp:Repeater ID="repNewsList" runat="server">
    
<ItemTemplate>
        
<tr>
            
<td align="center"><a href="list.aspx?caid=<%# Eval("caId") %>"><%# Eval("name"%></a></td>
            
<td align="center"><%# Eval("createTime"%></td>
        
</tr>
    
</ItemTemplate>
</asp:Repeater>  

然后在绑定代码下方加上分页控件(当然这个可以随便放,怎么好看怎么放):
ContractedBlock.gif ExpandedBlockStart.gif Code
<!--分页控件-->
        
<div style="text-align:center; height:50px; line-height:50px;">
        
            
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" UrlPaging="true" 
                FirstPageText
="首页" LastPageText="末页" NextPageText="下一页" NumericButtonCount="5" 
                onpagechanged
="AspNetPager1_PageChanged" PagingButtonSpacing="10px" NumericButtonTextFormatString="[{0}]"
                PrevPageText
="上一页" SubmitButtonText="Go" TextAfterPageIndexBox="" 
                TextBeforePageIndexBox
="转到" ShowCustomInfoSection="Left" 
                CustomInfoHTML
="目前是第%CurrentPageIndex%页 / 总共%PageCount%页">
            
</webdiyer:AspNetPager>
            
        
</div>

最后在aspx.cs中加上数据的分页绑定方法(这里的SQL语句要根据列表显示的需要进行调整):
ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
    
/// 绑定带有分页的新闻列表
    
/// </summary>

    public void BindRepeater()
ExpandedBlockStart.gifContractedBlock.gif    
{
        
int caid = int.Parse(Request.QueryString["caid"]);

        
string Sql = "select * from news where caId=" + caid + " order by createTime desc";
        
int CurrentPage = AspNetPager1.CurrentPageIndex;
        
int PageSize = AspNetPager1.PageSize;
        
int RecordCount;

        DataSet ds 
= PageManager.GetPage(Sql, CurrentPage, PageSize, out RecordCount);
        AspNetPager1.RecordCount 
= RecordCount;

        AspNetPager1.CustomInfoHTML 
+= " 共" + RecordCount + "条新闻</b>";

        repNewsList.DataSource 
= ds;
        repNewsList.DataBind();
    }

别忘了,在page_load调用一下:
protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
{
        
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            BindRepeater();
        }

    }

还有分页控件的PageChanged事件里也调用一下:
protected   void  AspNetPager1_PageChanged( object  sender, EventArgs e)
    {
        BindRepeater();
    }

最后再补充一个非常漂亮的翻页样式,清爽超酷型~:
ContractedBlock.gif ExpandedBlockStart.gif Code
<style>
    
    .anpager 
ExpandedBlockStart.gifContractedBlock.gif    
{}{
        font-size
:12px;
    
}

    .anpager .cpb 
ExpandedBlockStart.gifContractedBlock.gif    
{}{
        background
:#1F3A87 none repeat scroll 0 0;
        border
:1px solid #CCCCCC;
        color
:#FFFFFF;
        font-weight
:bold;
        margin
:5px 4px 0 0;
        padding
:4px 5px 0;
    
}

    .anpager a 
ExpandedBlockStart.gifContractedBlock.gif    
{}{
        background
:#FFFFFF none repeat scroll 0 0;
        border
:1px solid #CCCCCC;
        color
:#1F3A87;
        margin
:5px 4px 0 0;
        padding
:4px 5px 0;
        text-decoration
:none
    
}

    .anpager a:hover
ExpandedBlockStart.gifContractedBlock.gif    
{}{
        background
:#1F3A87 none repeat scroll 0 0;
        border
:1px solid #1F3A87;
        color
:#FFFFFF;
    
}


</style>

然后在AspNetPager中加上以下四个属性,搞定!
CssClass="anpager"
CurrentPageButtonClass="cpb"
CustomInfoClass=""
CustomInfoTextAlign="Left"

OK,分页大功告成。 本人QQ:3053166 希望和.net爱好者交流~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个分页控件名为AspNetPager控件,是Asp.net上使用率最高的分页控件,想怎么分就怎么分.附带'超详细代码注释",好用请给评论. 基本步骤: 1.拖拽控件(存放到到Bin目录下,再拖入工具箱) 2.粘贴复制(已放出实例源码) 3.修改Sql语句,即可使用. 特性如下: 强大的各种属性,附带多种CSS,可自定义CSS,想怎么分就怎么分页! 上下页,1234分页,首尾分页,页面跳转,等等,统统一步搞定. 实例代码(包内也有): ___________________________________________________________________ Default.aspx页面↓↓ ___________________________________________________________________ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!--分页控件命名--> <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> <!--分页控件命名--> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <%# Eval("ID") %> </ItemTemplate> </asp:DataList> <!--分页控件开始--> <webdiyer:AspNetPager ID="Pager1" runat="server" PageSize="8" CssClass="anpager" OnPageChanged="AspNetPager1_PageChanged" FirstPageText="首页 |" LastPageText="| 尾页" NextPageText="下一页" PrevPageText="上一页" ShowPageIndexBox="Always" AlwaysShow="True" Font-Size="13px"> </webdiyer:AspNetPager> <!--分页控件结束--> </div> </form> </body> </html> ____________________________________________________________ Default.aspx.cs页面代码↓↓ ____________________________________________________________ using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; //引用命名空间 using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { string connstring = "server=baiyi-js\\SQL2005;uid=sa;pwd=123456;database=xcbaiyi";//修改数据库连接字符串(必须改) protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlConnection conn = null; try { conn = new SqlConnection(connstring); conn.Open(); SqlCommand Count = new SqlCommand(); Count.Connection = conn; Count.CommandText = "select count(*) from tuiguang_2"; //Sql查询语句(必修改) Pager1.RecordCount = (int)Count.ExecuteScalar(); //"Pager1"为分页控件ID.在工具箱拖拽添加控件,同时会在aspx页面顶部添加控件命名控件(无需修改) BindData(); } finally { conn.Close(); } } } //绑定数据-2_只修改Sql语句即可 public void BindData() { SqlConnection conn = new SqlConnection(connstring); string sql = "select * from tuiguang_2";//Sql查询语句(必修改) SqlDataAdapter da = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); da.Fill(ds, Pager1.PageSize * (Pager1.CurrentPageIndex - 1), Pager1.PageSize, "temptbl"); DataTable dt = ds.Tables["temptbl"]; DataList1.DataSource = dt; DataList1.DataBind(); } //控件事件-每次重新绑定_不需修改 protected void AspNetPager1_PageChanged(object src, EventArgs e) { BindData(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值