本文實(shí)例講述了asp.net利用后臺(tái)實(shí)現(xiàn)直接生成html分頁(yè)的方法,是一個(gè)比較實(shí)用的功能。分享給大家供大家參考之用。具體方法如下:
1.建立存儲(chǔ)過(guò)程:
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
目前實(shí)驗(yàn)的是每頁(yè)5條數(shù)據(jù),傳入當(dāng)前頁(yè)碼就可以了
2.前臺(tái)定義一個(gè)div ,用于顯示
div id="divPage" runat="server" class="divpage">
/div>
3.后臺(tái)根據(jù)傳入?yún)?shù)來(lái)拼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() + "\">上一頁(yè)/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() + "\">下一頁(yè)/a>");
}
divPage.InnerHtml = sbr.ToString();
rptNews.DataSource = pageLogic.QueryNews(IPage);
rptNews.DataBind();
4.測(cè)試效果如下圖所示:
希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- asp.net獲取HTML表單File中的路徑的方法
- asp.net 中將表單提交到另一頁(yè) Code-Behind(代碼和html在不同的頁(yè)面)
- Asp.net動(dòng)態(tài)生成html頁(yè)面的方法分享
- 使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
- jquery獲取ASP.NET服務(wù)器端控件dropdownlist和radiobuttonlist生成客戶端HTML標(biāo)簽后的value和text值
- ASP.net(c#) 生成html的幾種解決方案[思路]
- 使用ASP.NET 2.0 CSS 控件適配器生成CSS友好的HTML輸出
- asp.net生成HTML
- 利用ASP.NET技術(shù)動(dòng)態(tài)生成HTML頁(yè)面
- asp.net 防止用戶通過(guò)后退按鈕重復(fù)提交表單
- asp.net 模擬提交有文件上傳的表單(通過(guò)http模擬上傳文件)
- ASP.NET中實(shí)現(xiàn)把form表單元素轉(zhuǎn)為實(shí)體對(duì)象或集合
- asp.net動(dòng)態(tài)生成HTML表單的方法