asp.net利用后台实现直接生成html分页的方法

这是初夏,陽光灿烂,海水湛蓝,海鸥轻巧地向船桅间滑过,生命是多彩的,充满了青春的欢乐。

本文实例讲述了asp.net利用后台实现直接生成html分页的方法,是一个比较实用的功能。分享给大家供大家参考之用。具体方法如下:

1.建立存储过程:

ALTER procedure [dbo].[p_news_query]
@Page int
as
begin
select top 5 new_id,new_title,new_url,new_content_text,create_time,user_name from 
 (select *,ROW_NUMBER() over(order by new_id desc ) as RowNumber  from
    (select new_id,new_title,new_url,new_content_text,
        dbo.f_ConvertDate(a.create_time) create_time, b.user_name from xs_new a
        left join xs_users b on b.user_no=a.create_user
)TI 
) A 
 where A.RowNumber>(@Page-1)*5

end

目前实验的是每页5条数据,传入当前页码就可以了

2.前台定义一个div ,用于显示

 <div id="divPage" runat="server" class="divpage">
  </div>

3.后台根据传入参数来拼html

目前是模仿博客园的模式,前后显示5条,多余的加...后到最后一条。具体代码如下:

  StringBuilder sbr = new StringBuilder();
  int ITotalCount = pageLogic.QueryNewsCount();
  int IPage = 1;
  if (Request["p"] != null)
  {
 IPage = Convert.ToInt32(Request["p"]);
  }
  int IPageCount = 5;
  int ITotalPage = ITotalCount / IPageCount;
  if (ITotalCount % IPageCount > 0)
  {
 ITotalPage += 1;
  }

  if (IPage != 1)
  {
 sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + (IPage - 1).ToString() + "\">上一页</a>");
  }

  if (ITotalPage <= 10)
  {
 for (int i = 1; i <= ITotalPage; i++)
 {
   if (i == IPage)
   {
 sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\" class=\"pageSelect\">" + i.ToString() + "</a>");
   }
   else
   {
 sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\">" + i.ToString() + "</a>");
   }
 }
  }
  else
  {

 if (IPage - 5 > 2 && IPage + 7 < ITotalPage)//前后都有的
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=1\">1</a>...");
   for (int i = IPage - 5; i <= IPage + 5; i++)
   {
 if (i == IPage)
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\" class=\"pageSelect\">" + i.ToString() + "</a>");
 }
 else
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\">" + i.ToString() + "</a>");
 }

   }
   sbr.Append("...<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + ITotalPage.ToString() + "\">" + ITotalPage.ToString() + "</a>");

 }
 else if (IPage - 5 <= 2)//前面不够,后面有多余
 {
   for (int i = 1; i <= 10; i++)
   {
 if (i == IPage)
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\" class=\"pageSelect\">" + i.ToString() + "</a>");
 }
 else
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\">" + i.ToString() + "</a>");
 }
   }
   sbr.Append("...<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + ITotalPage.ToString() + "\">" + ITotalPage.ToString() + "</a>");
 }
 else if (IPage + 7 >= ITotalPage)//前面多余,后面不足
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=1\">1</a>...");
   for (int i = ITotalPage - 10; i <= ITotalPage; i++)
   {
 if (i == IPage)
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\" class=\"pageSelect\">" + i.ToString() + "</a>");
 }
 else
 {
   sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + i.ToString() + "\">" + i.ToString() + "</a>");
 }
   }

 }

  }
  if (IPage != ITotalPage)
  {
 sbr.Append("<a rel="nofollow noopener noreferrer" href=\"news.aspx?p=" + (IPage + 1).ToString() + "\">下一页</a>");
  }
  divPage.InnerHtml = sbr.ToString();
  rptNews.DataSource = pageLogic.QueryNews(IPage);
  rptNews.DataBind();

4.测试效果如下图所示:

希望本文所述对大家的asp.net程序设计有所帮助。

以上就是asp.net利用后台实现直接生成html分页的方法。时间就象海绵里的水一样,只要你愿意挤,总还是有的。更多关于asp.net利用后台实现直接生成html分页的方法请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
ASP.NET中Response.BufferOutput属性的使用技巧

ASP.NET轻量级MVC框架Nancy的基本用法

使用grpcui测试ASP.NET core的gRPC服务

ASP.NET Core中的对象池介绍

.NET集成ORM框架HiSql