彩票走势图

用AspNetPager控件最详细的分页实现方法

转帖|其它|编辑:郝浩|2010-10-27 14:06:22.000|阅读 843 次

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

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  之前都是用GridView自带的分页,版式难看不说,还极不优化,一次取出所有记录,然后进行假分页。

  现在用aspNetPager控件做出的真分页,就好多了,不过还有改进的地方,SQL语句如果换成存储过程效率会更高。

  首先在SqlHelper.cs(DAL层中的数据库助手类,用于写可以复用的基本增删查改方法)中加上以下代码:

  Code

  /**//// <summary>

  /// 获取分页数据

  /// </summary>

  /// <param name="sql">sql语句</param>

  /// <param name="currentPage">当前页</param&gt;

  /// <param name="pagesize">每页显示数&lt;/param>

  /// <param name="recordcount"><;/param>

  /// <returns></returns>

  public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)

  {

  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)

  {

  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层方法:

  Code

  /**//// <summary>

  /// 获取分页数据

  /// </summary>

  /// <param name="sql">sql语句</param>

  /// <param name="currentPage">当前页</param>

  /// <param name="pagesize&quot;>每页显示数</param>

  /// &lt;param name=&quot;recordcount"></param>

  /// <returns></returns>

  public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)

  {

  return SQLHelper.GetPage(sql, currentPage, pagesize, out recordcount);

  }

  asp.net需分页的数据绑定处是这样的:

  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>

然后在绑定代码下方加上分页控件(当然这个可以随便放,怎么好看怎么放):

  Code

  <!--分页控件-->

  <div style="text-align:center; height:50px; line-height:50px;">

  <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow=&quot;True" UrlPaging="true"

  FirstPageText="彩票走势图" LastPageText="末页" NextPageText=";下一页&quot; NumericButtonCount="5"

  onpagechanged=&quot;AspNetPager1_PageChanged" PagingButtonSpacing="10px" NumericButtonTextFormatString="[{0}]"

  PRevPageText="上一页" SubmitButtonText=&quot;Go" TextAfterPageIndexBox=&quot;页"

  TextBeforePageIndexBox="转到" ShowCustomInfoSection="Left"

  CustomInfoHTML="目前是第%CurrentPageIndex%页 / 总共%PageCount%页">

  </webdiyer:AspNetPager>

  </div>

  最后在aspx.cs中加上数据的分页绑定方法(这里的SQL语句要根据列表显示的需要进行调整):

  Code

  /**//// <summary>

  /// 绑定带有分页的新闻列表

  /// </summary>

  public void BindRepeater()

  {

  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)

  {

  if (!Page.IsPostBack)

  {

  BindRepeater();

  }

  }

  还有分页控件的PageChanged事件里也调用一下:

  protected void AspNetPager1_PageChanged(object sender, EventArgs e)

  {

  BindRepeater();

  }

  最后再补充一个非常漂亮的翻页样式,清爽超酷型~:

  Code

  <style>

  .anpager

  {}{

  font-size:12px;

  }

  .anpager .cpb

  {}{

  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

  {}{

  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

  {}{

  background:#1F3A87 none repeat scroll 0 0;

  border:1px solid #1F3A87;

  color:#FFFFFF;

  }

  </style>

  然后在AspNetPager中加上以下四个属性,搞定!

  CSSClass="anpager"

  CurrentPageButtonClass="cpb"

  CustomInfoClass=""

  CustomInfoTextAlign="Left"


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP