asp.net利用后台实现直接生成html分页的方法
程序员文章站
2024-02-24 17:22:10
本文实例讲述了asp.net利用后台实现直接生成html分页的方法,是一个比较实用的功能。分享给大家供大家参考之用。具体方法如下:
1.建立存储过程:
alte...
本文实例讲述了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 href=\"news.aspx?p=" + (ipage - 1).tostring() + "\">上一页</a>"); } if (itotalpage <= 10) { for (int i = 1; i <= itotalpage; i++) { if (i == ipage) { sbr.append("<a href=\"news.aspx?p=" + i.tostring() + "\" class=\"pageselect\">" + i.tostring() + "</a>"); } else { sbr.append("<a href=\"news.aspx?p=" + i.tostring() + "\">" + i.tostring() + "</a>"); } } } else { if (ipage - 5 > 2 && ipage + 7 < itotalpage)//前后都有的 { sbr.append("<a href=\"news.aspx?p=1\">1</a>..."); for (int i = ipage - 5; i <= ipage + 5; i++) { if (i == ipage) { sbr.append("<a href=\"news.aspx?p=" + i.tostring() + "\" class=\"pageselect\">" + i.tostring() + "</a>"); } else { sbr.append("<a href=\"news.aspx?p=" + i.tostring() + "\">" + i.tostring() + "</a>"); } } sbr.append("...<a 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 href=\"news.aspx?p=" + i.tostring() + "\" class=\"pageselect\">" + i.tostring() + "</a>"); } else { sbr.append("<a href=\"news.aspx?p=" + i.tostring() + "\">" + i.tostring() + "</a>"); } } sbr.append("...<a href=\"news.aspx?p=" + itotalpage.tostring() + "\">" + itotalpage.tostring() + "</a>"); } else if (ipage + 7 >= itotalpage)//前面多余,后面不足 { sbr.append("<a href=\"news.aspx?p=1\">1</a>..."); for (int i = itotalpage - 10; i <= itotalpage; i++) { if (i == ipage) { sbr.append("<a href=\"news.aspx?p=" + i.tostring() + "\" class=\"pageselect\">" + i.tostring() + "</a>"); } else { sbr.append("<a href=\"news.aspx?p=" + i.tostring() + "\">" + i.tostring() + "</a>"); } } } } if (ipage != itotalpage) { sbr.append("<a href=\"news.aspx?p=" + (ipage + 1).tostring() + "\">下一页</a>"); } divpage.innerhtml = sbr.tostring(); rptnews.datasource = pagelogic.querynews(ipage); rptnews.databind();
4.测试效果如下图所示:
希望本文所述对大家的asp.net程序设计有所帮助。